0% found this document useful (0 votes)
7 views

lecture 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

lecture 3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Principles of Object Oriented

Programming (CS2012)
Lecture 3
Fundamental Principles of OOP

1
OOP Concepts
• Object
• Class
• Inheritance
• Abstraction
• Encapsulation
• Polymorphism
• Cohesion and Coupling

2
Object
• An object has:

– state - descriptive characteristics

– behaviors - what it can do (or what can be done to it)

• The state of a bank account includes its account number and


its current balance

• The behaviors associated with a bank account include the


ability to make deposits and withdrawals

• Note that the behavior of an object might change its state

3
Class
• An object is defined by a class
• A class is the blueprint of an object
• Multiple objects can be created from the
same class

4
Fundamental Principles of OOP
• Inheritance
– Inherit members from parent class
• Abstraction
– Define and execute abstract actions
• Encapsulation
– Hide the internals of a class
• Polymorphism
– Access a class through its parent interface

5
Inheritance

6
Inheritance
• Classes define attributes and behavior
– Fields, properties, methods, etc.
– Methods contain code for execution

• Interfaces define a set of operations


– Empty methods and properties, left to be
implemented later

7
Inheritance
• Inheritance allows child classes to inherit the
characteristics of the parent class
– Attributes (fields and properties)
– Operations (methods)
• Child class can extend the parent class
– Add new fields and methods
– Redefine methods (modify existing behavior)
• A class can implement an interface by
providing implementation for all its methods
8
Inheritance

Base
derived class inherits class/parent
class

class implements interface

9
Inheritance
• Benefits of inheritance
– Extensibility
– Reusability
– Provides abstraction
– Eliminates redundant code
• Use inheritance to build is-a relationships
– E.g. dog is-a(n) animal (dogs are kind of animals)
• Don't use it to build has-a relationship
– E.g. dog has-a name (dog is not kind of name)

10
Inheritance
Base class/
Person Super Class
+Name: String
+Address: String

Derived class/ Derived class/


Sub Class Sub Class

Lecturer Student
+stud_id: String
+emp_id: String
+gpa: double
+Salary: double

11
Inheritance
• Inheritance leads to a hierarchy of classes
and/or interfaces in an application:
Game

SinglePlayerGame MultiplePlayersGame

Minesweeper Solitaire BoardGame …

Chess Backgammon 12
Java Class Hierarchy
• D:\teaching\2015\BSc\CS2012\lectures\lectur
e 4\Class Hierarchy (Java Platform SE 7 ).html

13
14
Inheritance – Java Example
public class Animal{
}
public class Mammal extends Animal{
}
public class Reptile extends Animal{
}
public class Dog extends Mammal{
}

15
Subclasses in Java
• A subclass inherits all of
the public and protected members of its
parent
• Fields (attributes)
– The inherited fields can be used directly, just like
any other fields.
– Can declare a field in the subclass with the same
name as the one in the super-class, thus hiding it
(not recommended).
– Can declare new fields in the subclass that are not
in the super class. 16
Subclasses in Java
• Methods
– Inherited methods can be used directly as they are
– Can have a new instance method in the subclass that has
the same signature as the one in the super class,
thus overriding it.
– Can have a new static method in the subclass that has the
same signature as the one in the super class, thus hiding it.
– Can declare new methods that are not in the super class.
• Can write a constructor that invokes the constructor of
the super class
– Implicitly
– By using the keyword super.
17
Subclasses in Java

18
Static (Class) Members
• Fields and methods that belong to the class

19
public class Bicycle {
private int speed;
private int id;
private static int numberOfBicycles = 0;

public Bicycle(int startSpeed) {


speed = startSpeed;
id = ++numberOfBicycles;
}
public int getID() {
return id;
}
public static int getNumberOfBicycles() {
return numberOfBicycles;
} 20
import java.lang.Math;

class Another {
public static void main(String[] args) {
int result;
//calling static method min by writing class name
result = Math.min(10, 20);

System.out.println(result);
}
}
21
Inheritance and Accessibility in Java
Modifier Class Package Subclass World
Public Y Y Y Y
Protected Y Y Y N
No modifier Y Y N N
private Y N N N

22
Interface – Java Example
interface Animal {
public void eat();
public void travel();
}

23
Interface – Java Example
public class MammalInt implements Animal{
public void eat(){
System.out.println("Mammal eats");
}
public void travel(){
System.out.println("Mammal travels");
}
public int noOfLegs(){
return 4;
}
} 24
Extending Interfaces – Java Example
public interface Sports {
public void setHomeTeam(String name);
public void setVisitingTeam(String name);
}
public interface Football extends Sports {
public void homeTeamScored(int points);
public void visitingTeamScored(int points);
public void endOfQuarter(int quarter);
}
public interface Hockey extends Sports {
public void homeGoalScored();
public void visitingGoalScored();
public void endOfPeriod(int period);
public void overtimePeriod(int ot);
} 25
Homework 
• Define an interface and create an instance of it
• Add a method to the interface
• Include some logic inside this method
• Add a constructor to the interface
• Add an instance field to the interface
• Add a static field to the interface
• Add a static final field to the interface
• Create another interface that extends the above
interface, and add a new method
• Create a class that implements this second interface
26
Homework 
• Create a class with instance methods, instance fields,
static methods, and static fields – two from each
• Access the instance variable inside the instance
method
• Access the class variable and class method inside the
instance method
• Access the class variable and the other class method
inside the class method
• Access instance variables and instance methods inside
the class methods
• Try to use the this keyword inside a class method

27
Homework 
• Create a new class A
• Create another class B that extends A
• Play with the access modifiers (slide 22) to make sure
you understand them
• Add an instance field to A and initialize it with some
value (inside the constructor)
• Add the same instance field to B initialize it with a
different value
• Create a new class C with the main method. Create an
instance of B in the main method. Print out the value
of the instance field

28
Homework 
• Add a method to A that prints out “I’m of type A”
• Add a method with the same signature to B that
prints out “I’m of type B”
• Create an instance of B in the main method class
C. Call the method you declared in A and print
the value
• How can you get your program to print both “I’m
of type A” and “I’m of type B”?
• Make the methods static and see what happens.
Note that you should call the method on the
class name 29
Homework 
• Modify the constructors in A and B so that
they print “A” and “B” respectively
• Execute the program and see what happens.
What can you deduce from the output you
see?
• Now add the line super(); to the constructor
of A. What happens if this line is added as the
last line inside the constructor?

30

You might also like