inheritance
AGENDA
01 what is Inheritance? 02 Why using Inheritance?
03 How to use Inheritance?
04 types of Inheritance
05 Constructor & Super?
01
What is
Inheritance?
What is Inheritance?
Inheritance is an important pillar of OOP. It is the mechanism in Java by which one class is allow to inherit
the featuers (fields & methods) of another class.
Important terminology:
Super Class : The class whose features are inherited is known as superclass(or a base
class or a parent class).
Sub Class: The class that inherits the other class is known as a subclass(or a derived
class, extended class, or child class). The subclass can add its own fields and methods in
addition to the superclass fields and methods.
02
Why Using
Inheritance?
Why using inheritance?
- The most important use is the reusability of code. The code that is present in the
parent class doesn’t need to be written again in the child class.
- To achieve runtime polymorphism through method overriding. We will learn
more about polymorphism in later session.
Reusability:-
Inheritance supports the concept of “reusability”, i.e. when we want to create a
new class and there is already a class that includes some of the code that we
want, we can derive our new class from the existing class. By doing this, we are
reusing the fields and methods of the existing class.
03
How To Use
Inheritance?
extends Keyword :-
extends : is the keyword used to inherit the properties of a class. Following is the syntax of
extends keyword.
Super {
.....
.....
}
Sub Super {
.....
.....
}
Example
04
Types Of Inheritance
05
Constructor & Super
super
The super keyword in Java is a reference variable which is used to refer
immediate parent class object.
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 c
super is used to refer immediate parent class instance variabl :
When you have a variable in child class which is already present in the parent class then in order to access
the variable of parent class, you need to use the super keyword.
Accessing the num variable of parent class:
By calling a variable like this super.variable_name , we can access the variable
of parent class if both the classes (parent and child) have same variable.
super can be used to invoke immediate parent class method :
The super keyword can also be used to invoke parent class method. It should be used if subclass contains
the same method as parent class.it is used if method is overridden.
by using super keyword super.method_name you can call the method of parent class.....
super can be used to invoke immediate parent class constructor :
When we create the object of sub class, the new keyword invokes the constructor of child class,
which implicitly invokes the constructor of parent class. parent class constructor is executed first
and then the child class constructor is executed. It happens because compiler itself adds super( ).
Parameterized super( ) call to invoke parameterized constructor of parent class
:
when we have a constructor in parent class that takes arguments then we can use
parameterized super, like super(100).
Note that, in the last example:
1- super( ) or parameterized super must be the first statement in constructor otherwise you will
get the compilation error: “Constructor call must be the first statement in a constructor” .
2- When we explicitly placed super in the constructor, the java compiler didn’t call the default
no-arg constructor of parent class.
THANKS
Any questions?