Chapter 4 Inheritance
Chapter 4 Inheritance
Definition
Inheritance in java is a mechanism in which a class acquires the properties of another class.
The idea behind inheritance in java is that you can create new classes that are built upon
existing classes. When you inherit from an existing class, you can reuse methods and fields of
parent class, and you can add new methods and fields also.
Inheritance represents the IS-A relationship, also known as parent-child relationship. Here a
class which is extended is known as Base class or Parent class or Super class the class which
is inheriting the properties is known as Derived class or Child class or Sub class.
The subclass can directly access the super class properties. Even it can access protected
members of super class from anywhere.
4.2. Use of inheritance (Why use inheritance in java)
For Code Reusability: The extended class does not have to repeat the existing properties
of its parent.
For changing the existing behaviour: If in case child needs to change the existing
behaviour of its parent then it can do by overriding the methods of its parent. Thus it
achieves run time polymorphism
Syntax:
Extends keyword indicates that you are making a new class that derives from an existing class.
Example: Programmer is the subclass and Employee is the superclass. Relationship between
two classes is Programmer IS-A Employee. It means that Programmer is a type of
Employee. But Employee is not a type of Programmer.
Output:
B
(a) Single Inheritance
Example:
Output:
(b) Multilevel Inheritance: Multilevel inheritance refers to a mechanism where a class can
inherit a derived class, thereby making this derived class the base class for the new class.
As you can see in below flow diagram C is subclass or child class of B and B is a child
class of A.
A
C
(b) Multilevel inheritance
Example: In this example we have three classes Car, Maruti and Maruti800. We have done
a setup class Maruti extends Car and class Maurit800 extends Maruti. With the help of this
multilevel hierarchy setup our Maurti800 class is able to use the methods of both the classes
(Car and Maruti).
Output:
(c) Hierarchical Inheritance: In such kind of inheritance one class is inherited by many sub
classes. In below example class B, C and D inherits the same class A. A is parent class
(or base class) of B, C & D.
A
B C D
(c) Hierarchical Inheritance
Output:
(d) Multiple Inheritances: The type of inheritance where child class may have multiple
parents but this type of inheritance is not supported by java.
(e) Hybrid inheritance: It is a combination of all types of inheritances but this is also not
supported by java.
Note: Java does not support Multiple Inheritance as if parents have method with same
name and same type signatures then it creates ambiguity when subclass tries to access
that method as it gets confused which method to call.
4.4. Use of super keyword
The subclass can refer its immediate super class by using super keyword.
There are three uses of super keyword:
(a) To refer super class constructor from subclass constructor
Syntax: super(parameters);
It should be first statement in sub class constructor.
Constructors can be referred only from other constructors
(b) To access super class instance variables from subclass
Syntax: super.variable-name
Use: generally it is used when subclass and superclass have same instance variable
name and subclass want to differentiate between those.
(c) To call super class methods from subclass
Syntax: super.method-name(parameters);
Use: generally it is used to call overridden methods of superclass from subclass.
Example:
Output:
Note: Superclass variable can refer subclass object but it cannot access subclass own
properties directly.
Example:
For that we need to again downcast it. Please refer up casting and down casting in the end of
this chapter.
4.5. Method Overriding
If a subclass want to change the original property of superclass. It can use the same method
signature and can add its own implementation. When in superclass as well as subclass, there is
a same method name and type signature then by default, a subclass method overrides the
superclass method. It is runtime polymorphism. Call to an overridden method is decided at run
time and call goes to subclass same method.
Example:
Output:
(b) Resolving at run time: Dynamic Method Dispatch: Calling the methods dynamically
by changing the object which is referred. We can resolve the call to an overridden method
at runtime by calling the methods dynamically, by referring the objects dynamically.
Hence there is no need of super keyword.
This way is used when
1. We want to achieve Run time polymorphism
2. We cannot change the implementation of subclass methods by adding the statement
of super.
Then the call to above methods from main will be:
4.7. Up casting and down casting
In up casting, children are casted with its parent and it down casting parent is casted with its
children. Super class variable can refer its subclass object directly in up casting, but subclass
variable cannot directly refer its super class object. If a parent is casted with its children, it
throws.
ClassCastException.
Example:
Up casting:
Down casting:
own as
composition.
Aggregation:
Aggregation.
Example:
Class Car
IS A
HAS A
Class Maruti Class Engine
Thus by inheritance we achieve IS-A relationship and if one object refers another object then
we achieve HAS-A relationship.
4.9. Difference between Method Overloading and Method Overriding
Access modifier and return type does not Access modifier and return type matters
matter while overloading the methods as while overriding the method. There are
compiler only checks the parameter certain rules regarding giving scope and
matching return type in sub class
1. Explain? What is Inheritances and Demonstrate How to Extend the class properties?
2. Explain the Different type of Inheritances with Mobile class example.
3. Show the use of super keyword in method and in class with suitable example slike
Vehicle.
4. Write the program for Restrict the class and method from further extend and overriding.
5. Implement the Is-A relationship in between Facebook class and tweeter and whatsup
class.
6. What is the calling sequence of constructors in inheritance?
7. Demonstrate the use of Dynamic Method Disptach in inheritance.
8. Implement the Has-a relationship in between department and trainer.