1|Page
Chapter Three
Inheritance
__________________________________________________________
What is inheritance:-
The process of defining new class from existing classes.
The process by which a class acquires all the properties of other classes.
It is the property which allows a child class to inherit some properties from its parent class. This is
achieved by using extends keyword.
Inheritance enables you to define a general class (e.g., a superclass) and later extend it to more
specialized classes (e.g., subclasses).
Inheritance allows a class to use the properties and methods of another class.
Only properties with access modifier public and protected can be accessed in child class.
Programming language refers to :-
1. Superclass as:-
Base class
Existing class
Parent class
General class
2. New class as
Subclass.
Derived class
Child class
Specialized
Extended class
A super-class can have any number of subclasses. But a subclass can have only one
superclass.
A subclass inherits accessible data fields and methods from its superclass and may
also add new data fields and methods.
A programmer can take an existing class and, without modifying it, add additional
features and capabilities to it. This is done by deriving a new class from the existing one.
2|Page
Advantages of Inheritance
__________________________________________________________
A. FOR CODE REUSABILITY
Once a class has been written, created, and debugged, it can be distributed to other programmers
for use in their own programs to avoid redundancy. This is called reusability.
Used for reusing software or code and this is done through relationship.
is-a relationship
The superclass and subclass have “is-a” relationship between them.
is-a represents the inheritance/extends“is part of”
is-a represents inheritance
In an is-a relationship, an object of a subclass can also be treated as an object of
its superclass—e.g., a car is a vehicle
has-a relationship
By contrast, has-a represents composition“is entirely made up of”
Has-a represents composition/aggregation
In a has-a relationship, an object contains as members references to other
objects— e.g., a car has a steering wheel (and a car object has a reference to a
steering-wheel object).
In Java, the class hierarchy begins with class Object (in package java.lang).
Each subclass can become a superclass for future subclasses.
Each arrow in the hierarchy represents an is-a relationship. As we follow the arrows upward in this class hierarchy, we
can state, for instance, that “an Employee is a Community Member” and “a Teacher is a Faculty member.”
Community Member is the direct superclass of Employee, Student and Alumnus and is an indirect superclass of all
the other classes in the diagram. Starting from the bottom, you can follow the arrows and apply the is-a relationship up
to the top most superclass. For example, an Administrator is a Faculty member, is an Employee, is a Community
Member and, of course, is an Object
3|Page
WAY OF INHERITANCE
__________________________________________________________
1. Single inheritance
2. Multiple Inheritance
1. Single inheritance
Java supports only single inheritance, in which each class is derived from exactly one direct super
class.
Note:-
Derived class B has its own properties + properties of A.
Derived Class B extends or inherits properties of super class A.
(Extends keyword Inherits properties of super class A in class B)
4|Page
Exercise:-
Single Inheritance example program in Java
Output
5|Page
Protected members
__________________________________________________________
1. Public mode
All public members of base class becomes public members of derived class.
All protected members of base class becomes protected in derived class.
All private members of base class are never accessed by the derived class members directly.
(If properties are declared inside the class or private mode it is impossible to access directly by being outside)
Private members (variables and methods) in the base class cannot be accessed from within any
method inside derived class.
Derived class member functions cannot directly access private members of their base class.
How to access the properties inside superclass for derived class
By making Name package private.
By making a method inside a base class and calling the method inside the subclass that
already made in base class.
2. Protected mode
All public and protected members of base class becomes protected members of derived class.
3. Private mode
All public and protected members of base class become private of derived class.
6|Page
Exercise
Java example using protected mode and public mode output is the same
Derived-class members can refer to public and protected members of the base class simply by using the
member name.
Output
7|Page
Exercise
Testing if a subclass can access the private members of a superclass
Accessed through making a method inside base class and calling the method inside the subclass
that already made in base class.
8|Page
Output
9|Page
2. Hierarchical inheritance
______________________________________________________
Problem:-Redundancy Solution:-Inheritance
_______________________________________________________
To avoid redundancy the best option is using Inheritance.
Example 1
Note:-
At superclass person we do have common attributes of the subclasses and each subclasses
extends or inherits the properties of superclass.
10 | P a g e
Example 2 :-
Then state properties of each object’s
11 | P a g e
How to create Inheritance
_____________________________________________________
By super class we can't access the properties of subclasses but the vice versa is true (by subclass we can access
the properties of superclass).
NOTE: - SUPER CLASS CAN ONLY ACCESS ITS PROPERTIES.
Accessing for the properties of Plain Bicycle we should only use its class name. All properties of Plain cycle can
be accessed by its actual class name and inherit the properties of superclass by the same class name.
Accessing for the properties of Geared cycle we should only use its class name. All properties of Geared cycle
can be accessed by its actual class name and inherit the properties of superclass by the same class name.
12 | P a g e
Output
13 | P a g e
B. FOR METHOD OVERRIDING (The other advantage of Inheritance )
When we declare the same method in child class which is already present in the
parent class this is called method overriding.
Whenever a parent and child class have same named methods overriding will be
occurred
Inheritance and Method Overriding
__________________________________________________________
super(),this() and final keyword
super() as well as this() both are used to make constructor calls.
super () calling for superclass
is used to call Base class’s constructor(i.e., Parent’s class) while
this () calling for current class
is used to call current class’s constructor
The final keyword
To avoid overridden of method definition by a new definition within derived class use final
keyword.(Prevent Overriding)
If you don’t want a class to be extended or if you don’t desire inheritance from superclass to
subclass use final keyword.
final method is inherited but you cannot override it.
14 | P a g e
Example which illustrates of final keyword and inheritance
Output
15 | P a g e
super Keyword in Java
__________________________________________________________
The super keyword in java is a reference variable that is used to refer parent class
objects.
When subclass method overrides superclass method,super keyword and dot(.)
separator is used to access the superclass method/variable/constructors.
Superclass's constructors are not inherited in the subclass. They can only be invoked using
the keyword super.
How to access data member without using super keyword.
Output
Or
16 | P a g e
Output
17 | P a g e
How to access superclass using super keyword.
Simply calling the members by creating a method (to call creating method is mandatory)
Output
18 | P a g e
Where we exactly use the “super” keyword?
__________________________________________________________
Note:-
When derived class and base class data members are the same always we should use super
keyword to call super class in derived class.
1. Use of super with variables:-
This scenario occurs when a derived class and base class has same data members. In that case
there is a possibility of ambiguity for the JVM. Therefore to solve the ambiguity use super keyword.
Exercise to illustrate super with variables
Output
19 | P a g e
Example on when derived class and base class has same data members
Output
20 | P a g e
2. Use of super with methods
Used when we want to call parent class method.
Whenever a parent and child class have same named methods then to resolve
ambiguity we use super keyword.
Output
21 | P a g e
3. Use of super with constructors
super keyword can also be used to access the parent class constructor.
One more important thing is that, ‘’super’ can call both parametric as well as non-parametric constructors
depending upon the situation.
You must use the keyword super to :-
To call the superclass constructor
To call a superclass method.
– You can use super like a function call in a derived class constructor to invoke the base
class constructor.
o super (); //invokes base class default constructor
o super(parameter); //invokes base class constructor with parameters
22 | P a g e
Exercise which illustrates constructor calling using super keyword.
Output
Output:
Person class Constructor
Student class Constructor