CPE207 Object Oriented Programming (Week 8)
CPE207 Object Oriented Programming (Week 8)
CPE207 Object Oriented Programming (Week 8)
2/10/2023 2
3
Need for Inheritance
4
Need for Inheritance
Parent class
6
2/10/2023 7
8
Definition
inheritance: a parent-child relationship between classes
2.For Method Overriding child class can override existing behavior from
parent
9
Inheritance: Need for & Definition & Terms
Type of Inheritance
Inheritance for Code Reusability
Inheritance with Constructor
◦ Super keyword
Inheritance for Method Overriding
Access Control
10
Types of Inheritance
11
In Java, you specify a class as your parent by using ‘extends’ keyword
public class Cat extends Animal {
12
Inheritance: Need for & Definition & Terms
Type of Inheritance
Inheritance for Code Reusability
Inheritance with Constructor
◦ Super keyword
Inheritance for Method Overriding
Access Control
13
A Cat object have A Dog object
have
color
color
age
breed
meow()
bark()
eat()
eat()
child class;
deep
BritishShortHair
MultiLevel inheritance
15
Inheritance: Need for & Definition & Terms
Type of Inheritance
Inheritance for Code Reusability
Inheritance with Constructor
◦ Super keyword
Inheritance for Method Overriding
Access Control
16
public Animal(String color){
this.color = color;
}
void printColor(){
System.out.println(this.color);
//prints color of Dog class
System.out.println(super.color);
//prints color of Animal class
}
}
18
used to refer to superclass (parent) of current class
can be used to refer to parent class's methods, variables,
constructors to call them
needed when there is a name conflict with current class
useful when overriding and you want to keep the old
behavior but add new behavior to it (method overriding)
syntax:
super(args); // call parent’s constructor
super.attributeName // access parent’s attribute
super.methodName(args); // access parent’s method
19
Inheritance: Need for & Definition & Terms
Type of Inheritance
Inheritance for Code Reusability
Inheritance with Constructor
◦ Super keyword
Inheritance for Method Overriding
Access Control
2/10/2023 20
In OOP, overriding means to override the functionality of an existing
method.
21
Method Overriding: Example
22
23
Inheritance: Need for & Definition & Terms
Type of Inheritance
Inheritance for Code Reusability
Inheritance with Constructor
◦ Super keyword
Inheritance for Method Overriding
Access Control
24
public: visible to all other classes
public class Animal
private: visible only to the current class, its methods, and every instance
(object) of its class
a child class cannot refer to its parent's private members!
private String name;
protected (this one's new to us): visible to the current class, and all of its
child classes
protected int age;
25
The following rules for inherited methods are enforced :
26
public class Child extends Parent {
public class Parent {
public int attribute4;
private int attribute1;
protected int attribute2;
public Child() { // Which are legal?
public int attribute3;
protected final static int attribute4 = 0; // _________
attribute1++; // _________
attribute5=1;
attribute2++; // _________
attribute3++; // _________
this.attribute1 = value;
setAttribute1(attribute4); // _____
}
}
} }
27
classes that have inheritance Attributes
relationships are connected by accessModifier name : type
arrows - for private
hierarchies drawn top-down with + for public
arrows from child to parent
# for protected
28
inheritance relationships associational relationships
(has a relationship)
(is a relationship)
1. multiplicity (how many)
◦ hierarchies drawn top-down
with arrows from child to 2. name (what relationship the objects
parent have)
3. navigability (who has relationship with
whom)
29
Lab exercise
30
31