Download - 5 Inheritance
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 1/19
Inheritance(Deriving Classes)
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 2/19
Inheritance is one of the most importantconcept in object-oriented programming, and it
has a direct effect on how you design and write
Java classes.
Inheritance is a powerful mechanism that
allows a class to inherit functionality from an
existing class.
For example, sports car, luxury car, both are
kinds of cars.
Inheritance
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 3/19
Car
Sports Car Luxury Car
Sports Car and Luxury Car are sub classes or child classes of Car class and Car class is the super or parent class of Luxury
Car and Sports Car.
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 4/19
Each subclass inherits all variables and methods
from the super class except private variables and
methods.
However, subclasses are not limited to the variables
and methods provided to them by their super class.
Subclasses can add variables and methods or can
change the definition of the existing methodsaccording to their own requirements.
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 5/19
We use extends keyword to create child classin java as follows:
class child-class extends parent-class
{
Class body…
}
Deriving Classes
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 6/19
class Shape
{
}
class Oval extends Shape
{
}
class Triangle extends Shape{
}
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 7/19
The object of child class can call any non
private method from the super class that is why
we can say that:
A child class object is a parent class object.
As there is no multiple inheritance in java so In
java every class can extend only one class.However one parent class may have more then
one child classes.
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 8/19
The topmost class in the java classhierarchy is called “Object”. If you
declare a class which does not subclass of any super class than your
class is considered to be the child
class of the class Object.
Java Class Hierarchy
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 9/19
Java Class Hierarchy
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 10/19
When you create an object of child class thenchild class constructor first calls the parent
class default constructor before it performs its
own initialization.
The parent class constructor calls its super
class constructor and so on.
This is necessary because if the parent class isnot properly initialize how the child class
object can be created properly.
Constructor Calling Order
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 11/19
If you don’t want to invoke the default constructor
of super class but want other constructor than you
use super to invoke the parent class constructor.
super(4);
This will invoke the parent class constructor that
would take one int argument. Call to parent class
constructor with super would be the first statement
in the child class constructor.
super
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 12/19
You can also invoke any method or variable of
super class in the child class with the super
keyword.
super.methodname( );
super.variablename = 7;
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 13/19
Method
Overriding
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 14/19
Method Overriding
When a child class defines a method withthe same name and signature as the parent,
then it is said that the child’s version
overrides the parent’s version in his favor.
When an overridden method is called from
child class object, the version in the child
class is called not the parent class version.
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 15/19
The return type, method name, number and
type of the parameters of overridden method
in the child class must match with themethod in the super class.
You can call the super class method fromthe child class with super keyword.
super.overriddenMethodName( );
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 16/19
Final
Keyword
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 17/19
final keyword
The keyword final has three usages:
1. Final variables.
2. Final methods.
3. Final classes.
1.Final variables
Final modifier used with the variables specify that
the variable has a constant value and it can notchanged.
final int age = 10;
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 18/19
2.Final methods
When we use the keyword final with methodsthen it specifies that the method cannot be
overridden in the child class.
final void volume( )
{
}
7/28/2019 5 Inheritance
http://slidepdf.com/reader/full/5-inheritance 19/19
3.Final classes
The keyword final can be applied to classes.If this is done, the class cannot be inherited.
This provides security features and stops
further extensions of the class.
final class City
{
}