JAVA-Module-3
Inheritance
CHANNAVEERAYA WM
Assistant Professor, Department of CS&E
The National Institute of Engineering, Mysuru
[Link]/in/channaveeraya-wujjini-matada-9323618
(Ex HCL-Perot Systems, Ex Siemens, Ex CSR(Qualcomm), Ex Dell, Ex Ericsson, Ex Ambient
Scientific Inc )
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 2
Inheritance
Read Chapter 8 of
OOPs Concepts/Themes - Inheritance
• Inheritance is an important pillar of
OOP(Object-Oriented Programming).
• The ability to create a new class from an
existing class is called Inheritance.
• The capability of a class to derive properties
and characteristics from another class is
called Inheritance.
• When we write a class, we inherit properties
from other classes. So when we create a
class, we do not need to write all the
properties and functions again and again, as
these can be inherited from another class that
possesses it.
• Inheritance allows the user to reuse the code
whenever possible and reduce its
redundancy.
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 4
OOPs Concepts/Themes - Inheritance-Types
• 4-Types of Inheritance
• Single Inheritance
• Multi-level Inheritance
• Hierarchical Inheritance
• Multiple Inheritance( With the help of interfaces)
[Link]
[Link]
• Importance of Inheritance in Java
• Code Reusability: Inheritance helps in reducing the rewriting of code.
i.e. Code can be reused by other classes, and the child class only has to
write its own unique properties. This reduces the time consumption and
complexity of the code.
• Method Overriding: The child class declares a method that is already
present in the parent class, then it is known as method overriding.
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 5
OOPs Concepts/Themes - Inheritance-Types
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 6
OOPs Concepts/Themes - Inheritance-Types
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 7
OOPs Concepts/Themes - Inheritance-Types
• Single Inheritance
• i.e. Fruit is the superclass, and Apple is the
subclass that extends the properties and
behavior of the Fruit class.
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 8
OOPs Concepts/Themes - Inheritance-Types
• Single
Inheritance
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 9
OOPs Concepts/Themes - Inheritance-Types
• Multilevel
Inheritance
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 10
OOPs Concepts/Themes - Inheritance-Types
• Hierarchical
Inheritance
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 11
OOPs Concepts/Themes - Inheritance-Types
• Hierarchical Inheritance
• In Hierarchical inheritance, a base class has more than one child
class, which means different classes acquire the class’s properties.
• For example, a class vehicle has subclasses cars, bikes, scooters.
• [Link]
in-java/
• [Link]
[Link]
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 12
OOPs Concepts/Themes - Polymorphism
• Poly means Many and morphism means Forms
• The generally accepted definition for this term in object oriented
programming is the capability of objects belonging to the same class
hierarchy to react differently to the same method/function call.
• This means that a function may be defined/implemented in different forms
with the same function name.
• Two types of Polymorphism
• Compile-Time/Static Polymorphism implemented via Method/Function Overloading also
called as early-binding.
• Run-Time/Dynamic Polymorphism implemented via Method/Function Overriding also called
as late-binding or dynamic-binding. Runtime polymorphism in java is also known as Dynamic
Binding which is used to call an overridden method that is resolved dynamically at runtime rather than
at compile time.
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 13
OOPs Concepts/Themes - Compile-Time Polymorphism
• Overloading by changing the number of
parameters
• Overloading by changing the data type
of parameters
[Link]
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 14
OOPs Concepts/Themes - Compile-Time Polymorphism
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 15
OOPs Concepts/Themes - Run-Time Polymorphism
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 16
OOPs Concepts/Themes - Run-Time Polymorphism -Examples
• In the above example, we have created a
superclass: Polygon and two subclasses: Square
and Circle. Notice the use of the render()
method.
• he main purpose of the render() method is to
render the shape. However, the process of
rendering a square is different than the process
of rendering a circle.
• Hence, the render() method behaves differently
in different classes. Or, we can say render() is
polymorphic.
[Link]
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 17
OOPs Concepts/Themes - Run-Time Polymorphism -Examples
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 18
OOPs Concepts/Themes - Run-Time Polymorphism -Examples
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 19
OOPs Concepts/Themes - Run-Time Polymorphism -Examples
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 20
OOPs Concepts/Themes - Java Runtime Polymorphism Example: Bank
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 21
OOPs Concepts/Themes - Java Runtime Polymorphism Example: Bank
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 22
OOPs Concepts/Themes - Java Runtime Polymorphism Example: Shape
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 23
OOPs Concepts/Themes - Java Runtime Polymorphism Example: Animal
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 24
OOPs Concepts/Themes - Java Runtime Polymorphism Example: Animal
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 25
OOPs Concepts/Themes - Java Runtime Polymorphism with Multilevel Inheritance
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 26
OOPs Concepts/Themes - Java Runtime Polymorphism with
Multilevel Inheritance
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 27
OOPs Concepts/Themes - Static Binding and Dynamic Binding
• Connecting a method call to the method body is known as binding.
• There are two types of binding
• Static Binding (also known as Early Binding, Compile Time Polymorphism).
• Dynamic Binding (also known as Late Binding , Run Time Polymorphism).
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 28
OOPs Concepts/Themes - Java Runtime Polymorphism with Data Member – Not Possible
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 29
Java final Keyword
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 30
Java final Keyword – With variables
final with variables
• When a variable defined with
the final keyword, it becomes
a constant, and it does not
allow us to modify the value.
• The variable defined with the
final keyword allows only a
one-time assignment, once a
value assigned to it, never
allows us to change it again.
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 31
Java final Keyword - With Methods
final with methods
• When a method defined with
the final keyword, it does not
allow it to override.
• The final method extends to
the child class, but the child
class can not override or re-
define it.
• It must be used as it has
implemented in the parent
class.
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 32
Java final Keyword - With class
final with class
• When a class defined
with final keyword, it
cannot be extended
by any other class.
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 33
Java Super Keyword
• The super keyword in Java is a reference
variable which is used to refer immediate
parent class object.
• Whenever you create the instance of
subclass, an instance of parent class is
created implicitly which is referred by
super reference variable.
• Usage of Java super Keyword
• super can be used to refer immediate parent
class instance variable.
• super can be used to invoke immediate
parent class method.
• super() can be used to invoke immediate
parent class constructor.
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 34
Java Super Keyword
• super is used to refer immediate parent class instance variable.
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 35
Java Super Keyword
• super can be used to invoke
parent class method
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 36
Java Super Keyword
• super is used to invoke
parent class constructor.
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 37
Java Super Keyword
• super example: real use
• Let's see the real use of super
keyword.
• Here, Emp class inherits Person
class so all the properties of
Person will be inherited to Emp
by default.
• To initialize all the property, we
are using parent class
constructor from child class. In
such way, we are reusing the
parent class constructor.
27-12-2023 NIE 3rd Sem- CSE-D & CSE-AIML - CHANNAVEERAYA WM 38