Week 10
OOP Concepts: Inheritance
Dr. Nehad Ramaha,
Computer Engineering Department
Karabük Universities These Slides mainly adopted from Assist. Prof. Dr. Ozacar Kasim lecture notes
1
The class notes are a compilation and edition from many sources. The instructor does not claim intellectual property or ownership of the lecture notes.
Inheritance: Definition & Terms
Types of Inheritance
Inheritance for Code Reusability
Inheritance with Constructor
◦ Super keyword
Inheritance for Method Overriding
Access Control
11/29/2021 2
3
Need for Inheritance
4
Need for Inheritance
Parent class
child class 5
child class
superclass, base class, parent class:
terms to describe the parent in the
relationship, which shares its
functionality
subclass, derived class, child class:
terms to describe the child in the
relationship, which accepts
functionality from its parent
extend, inherit, derive:
become a subclass of another class
6
11/29/2021 7
8
Definition
inheritance: a parent-child relationship between classes
Why use inheritance in java?
1. For Code Reusability allows to reuse variables and methods of the
existing class when you create a child from it.
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 {
A Java child class has exactly one parent
◦ Some other languages (C++) allow multiple inheritance
by default, a class's parent is Object
constructors are not inherited
◦ because they are not members of a class
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()
We reuse codes that in Parent class
Let's move to Netbeans! 14
it is possible to extend a class that itself is a Animal
child class;
inheritance chains like this can be arbitrarily Cat
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;
}
Remark: if the superclass has a constructor that requires any
arguments (not default constructor),
you must put a constructor in the subclass and have it call
the super-constructor
(call to super-constructor must be the first statement)
17
class Animal{
String color="white"; public class Test {
} public static void main(String[] args) {
Dog d=new Dog();
d.printColor();
class Dog extends Animal{
}
String color="black";
}
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
11/29/2021 20
In OOP, overriding means to override the functionality of an existing
method.
Child class can replace behavior of its parent's methods by
redefining them
a subclass can implement a parent class method based on its
requirement
21
Method Overriding: Example
you have already done this ...
where?
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 :
1. Public methods in a superclass also must be public in all
subclasses.
2. Protected methods in a superclass must be protected or
public in subclasses; they cannot be private.
3. Private methods are not inherited at all, so there is no rule for
them.
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; // _________
attribute5=1; attribute1++; // _________
attribute2++; // _________
attribute3++; // _________
private void method1() {} attribute5++; // _________
public void method2() {}
protected void setAttribute1(int super.method1(); // _________
value){ method2(); // _________
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
30
31
32