OOP-Chapter 3
OOP-Chapter 3
INHERITANCE
4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this() and super()
6. Use of final keyword with inheritance
7. Constructors in subclasses
4/6/2024
Introduction
4
4/6/2024
5
Car
4/6/2024
Is-a relationship
6
Surgeon is a Doctor
Dog is an Animal
4/6/2024
Type of Inheritance
8
Hierarchical inheritance
Multilevel inheritance
4/6/2024
9
Single Inheritance
In single inheritance, a single subclass extends from a
single superclass.
For example,
4/6/2024
10
Multilevel Inheritance
Inmultilevel inheritance, a subclass extends from a
superclass and then the same subclass acts as a
superclass for another class.
For example
4/6/2024
11
Hierarchical Inheritance
In hierarchical inheritance, multiple subclasses extend
from a single superclass.
For example,
4/6/2024
12
Multiple Inheritance
Inmultiple inheritance, a single subclass extends from
multiple super classes.
Note: Java doesn't support multiple inheritance
For example,
4/6/2024
13
Hybrid Inheritance
Hybrid inheritance is a combination of two or more
types of inheritance.
For example,
4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this() and super()
6. Use of final keyword with inheritance
7. Constructors in subclasses
4/6/2024
Super and Sub Classes
15
}
4/6/2024
Example
17
4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this() and super()
6. Use of final keyword with inheritance
7. Constructors in subclasses
4/6/2024
Protected Members
20
4/6/2024
21
4/6/2024
22
4/6/2024
23 4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this() and super()
6. Use of final keyword with inheritance
7. Constructors in subclasses
4/6/2024
Inheritance and Method Overriding
25
4/6/2024
26
4/6/2024
27
Example:
4/6/2024
28
4/6/2024
29
4/6/2024
30
4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this and super keyword
6. Use of final keyword with inheritance
7. Constructors in subclasses
4/6/2024
Using this and super keyword
32
4/6/2024
33
this Keyword
It is an implicit reference variable that is used to refer
current class objects
It invokes the constructor of subclass
In the case of overridden methods, this keyword is used
to invoke the method of subclass
It solves the problem of variable hiding. Using this
keyword, a compiler always refers to instance variable
of the current class.
It can be used to return and pass as an argument in
context of current class object
4/6/2024
34
super keyword
It is an implicit reference variable that is used to refer
child class’s immediate parent class objects.
It invokes the constructor of superclass
Variable Hiding
When both the child and the parent classes have a
variable with the same name, the child's variable
hides the one from the parent.
4/6/2024
36 4/6/2024
37
4/6/2024
38
Method Hiding
Method hiding may happen in any hierarchy structure in
java.
When a child class defines a static method with the
same signature as a static method in the parent class,
then the child's method hides the one in the parent class.
The same behavior involving the instance methods is
called method overriding.
4/6/2024
39 4/6/2024
40
4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this() and super()
6. Use of final keyword with inheritance
7. Constructors in subclasses
4/6/2024
final keyword
42
Example
class Parent {
final void final_method() {
//definition of the Final Method
}
}
class Child extends Parent {
final void final_method(){
//generates a compile-time error
}
}
4/6/2024
Contents
1. Introduction
2. Super-classes and sub-classes
3. Protected members
4. Overriding methods
5. Using this() and super()
6. Use of final keyword with inheritance
7. Constructors in subclasses
4/6/2024
Constructor in subclass
45
4/6/2024
46
4/6/2024
47
Example
public Box()
{
super();
height = 0;
}
4/6/2024
48
Questions?
4/6/2024
49
Thank You
4/6/2024