CHAPTER-3 Inheritance
CHAPTER-3 Inheritance
CHAPTER - 3
Inheritance
Objectives
Types of Inheritance
Benefits of inheritance
Aggregation
Review Questions
Inheritance
Parent
Inherited capability
Child
Inheritance
Car
Example
Declaring sub classes
subclass superclass
or extends or
derived class base class
Inheritance - example
Declaring Sub classes
Types of inheritance
A A B A
B C B C D
(a) Single Inheritance (b) Multiple Inheritance (c) Hierarchical Inheritance
A A A
B C B C
B
C D D
(d) Multi-Level Inheritance (e) Hybrid Inheritance (f) Multipath Inheritance
Single inheritance example
Single inheritance example (cont …)
Single inheritance example (cont …)
output
Multilevel Inheritance example
Multilevel Inheritance example(cont…)
output
Multiple inheritance
Variables and methods declared with public visibility are inherited; those
with private visibility are not
final keyword is used to declare constants which can not change its
value of definition.
Polymorphism came from the two Greek words „poly‟ means many and
morph means forms i.e. many forms
If the same method has ability to take more than one form to perform
several tasks then it is called polymorphism.
A polymorphic reference is a variable that can refer to different types of
objects at different points in time.
It is of two types: Static Polymorphism and Dynamic Polymorphism.
Static Polymorphism
Static Polymorphism:
The polymorphism exhibited at compile time is called Static
polymorphism.
Here the compiler knows which method is called at the
Writing two or more methods in super & sub classes with same name and same
signatures is called method overriding.
In method overriding JVM executes a method depending on the type of the
object.
Dynamic Polymorphism using method overriding
Overloading Vs Overriding
1. What is inheritance?
2. What is the purpose of super keyword?
3. Which class is at the top of the class hierarchy in Java?
4. What are the constructor issues surrounding inheritance?
5. What is method overriding?
6. Using aggregation, show the relationship between Address and
Employee class.
7. Describe the two types of polymorphisms with example
8. What is the purpose of “final” java key word
9. Describe basic properties of abstract classes.
Adama Science and Technology University
School of Electrical Engineering And Computing 2023 G.C