Inheritance
Lecture 12
Prof. Anita Agrawal,
BITS –Pilani, [Link] Goa
campus
2 2/26/2026 1:15 PM
What is inheritance?
In Java language, classes can be derived
from other classes, thereby inheriting fields
and methods from those classes.
A class that is derived from another class: (a
subclass/derived class/extended class/ child
class).
A class from which it is derived:
superclass/base class/extending class/parent
class
Anita Agrawal ECOM F213
3 2/26/2026 1:15 PM
Except for the class Object, every other class
has one and only one direct superclass (single
inheritance).
If there is no explicit superclass for a class, it is
implicitly a subclass of the class object.
Classes can be derived from classes which
are derived from classes, ……. And are
ultimately derived from class object.
Anita Agrawal ECOM F213
4 2/26/2026 1:15 PM
Anita Agrawal ECOM F213
5 2/26/2026 1:15 PM
A subclass inherits all the members* (fields,
methods, and nested classes) from its
superclass.
Constructors of the superclass are not
inherited by subclass, but can be invoked
from the subclass.
Anita Agrawal ECOM F213
6 2/26/2026 1:15 PM
A subclass inherits all of the public and protected
members of its parent, no matter what package the
subclass is in.
If the subclass is in the same package as its parent, it
also inherits the package-private members of the
parent.
A subclass does not inherit the private members of its
superclass.
However, if the superclass has public or protected
methods for accessing its private fields, these can also
be used by the subclass.
Anita Agrawal ECOM F213
7 2/26/2026 1:15 PM
Fields in subclasses
You can use the inherited members just as they are,
replace them, hide them, or supplement them with
new members.
You can declare a field in the subclass with the same
name as the one in the superclass, thus hiding it (not
recommended).
You can declare new fields in the subclass that are
not in the superclass.
Anita Agrawal ECOM F213
8 2/26/2026 1:15 PM
Methods in subclasses
The inherited methods can be used directly as they
are.
You can write a new instance method in the
subclass that has the same signature as the one in
the superclass, thus overriding it.
You can declare new methods in the subclass that
are not in the superclass.
Anita Agrawal ECOM F213
9 2/26/2026 1:15 PM
Constructors in subclasses
You can write a subclass constructor that
invokes the constructor of the superclass,
either implicitly or by using the keyword super.
Anita Agrawal ECOM F213
10 2/26/2026 1:15 PM
Types of inheritances in Java
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Multiple Inheritance (Through Interfaces)
Anita Agrawal ECOM F213
11 2/26/2026 1:15 PM
Anita Agrawal ECOM F213
12 2/26/2026 1:15 PM
Anita Agrawal ECOM F213
13 2/26/2026 1:15 PM
SingleInheritance: One class (sub class)
extends another class (superclass)
(one class only)
MultilevelInheritance: A derived class will be
inheriting a base class and the derived class will
act as the base class to other class.
MultipleInheritance: One class extends more
than one class. Java does not support multiple
inheritance through classes. It can support this
only through interfaces
Anita Agrawal ECOM F213
14 2/26/2026 1:15 PM
Hierarchical inheritance: One class is extended by
many subclasses.
Hybrid Inheritance: It is a combination of single
and multiple inheritance.
Summary:
In Java, when an "Is-A" relationship exists between
two classes, we use Inheritance.
The keyword ‘extend’ is used by the subclass to
inherit the features of the superclass.
Inheritance is important since it leads to
reusability of the code. Anita Agrawal ECOM F213
15 2/26/2026 1:15 PM
Method overriding ..revisited
Method in a subclass and a method in its
superclass has the same signature
…..overriding
Method defined by the superclass will be
hidden
Method defined by the subclass will be
called
Subclass is said to override the method in
the superclass
Anita Agrawal ECOM F213
16 2/26/2026 1:15 PM
• If a superclass contains a method that is overridden by
a subclass, then when different types of objects are
referred to through a superclass reference variable,
different versions of the method are executed.
• In Java, we can override methods only, not the
variables(data members), so runtime polymorphism
cannot be achieved by data members.
Anita Agrawal ECOM F213