5 Inheritance in JAVA
5 Inheritance in JAVA
CSA6003T
BHAWANA BOTHARA
ASSISTANT PROFESSOR
Learning Objective
Understand the concept of inheritance in Java.
Differentiate between different types of inheritance.
Implement inheritance in Java programs.
Recognize the benefits and limitations of inheritance.
Apply inheritance in real-world scenarios effectively.
Introduction to Inheritance
Inheritance is a mechanism in Java where one class acquires the properties (fields) and
behaviours (methods) of another class.
It promotes code reuse and establishes a parent-child relationship between classes.
In Java, inheritance is an is-a relationship. That is, we use inheritance only if there exists
an is-a relationship between two classes. For example,
Car is a Vehicle
Orange is a Fruit
Surgeon is a Doctor
Dog is an Animal
The syntax of Java Inheritance
Although a subclass includes all of the members of its superclass, it cannot access
those members of the superclass that have been declared as private.
Java Inheritance Types
Below are the different types of inheritance which are supported by Java.
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Multiple Inheritance
Hybrid Inheritance
Single Inheritance
In single inheritance, subclasses inherit the features of one superclass. In the
image below, class A serves as a base class for the derived class B.
Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting a base class, and as
well as the derived class also acts as the base class for other classes. In the below
image, class A serves as a base class for the derived class B, which in turn serves
as a base class for the derived class C. In Java, a class cannot directly access the
grandparent’s members.
Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for more
than one subclass. In the below image, class A serves as a base class for the
derived classes B, C, and D.
Multiple Inheritance (Through
Interfaces)
In Multiple inheritances, one class can have more than one superclass and inherit features from
all parent classes. Please note that Java does not support multiple inheritances with classes. In
Java, we can achieve multiple inheritances only through Interfaces.
Hybrid Inheritance
It is a mix of two or more of the above types of inheritance. Since Java doesn’t support multiple
inheritances with classes, hybrid inheritance involving multiple inheritance is also not possible with
classes. In Java, we can achieve hybrid inheritance only through Interfaces if we want to involve
multiple inheritance to implement Hybrid inheritance.
Access Specifiers
public: Accessible from any other class.
private: Accessible only within the declared class.
protected: Accessible within the same package and subclasses.
default: Accessible only within the same package.
Method Overriding in Java Inheritance
if the same method is present in both the superclass and subclass, what will
happen?
In this case, the method in the subclass overrides the method in the superclass.
This concept is known as method overriding in Java.
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java.
In other words, If a subclass provides the specific implementation of the method
that has been declared by one of its parent class, it is known as method overriding.
Usage of Java Method Overriding
2 Method overloading is performed within class. Method overriding occurs in two classes that have
IS-A (inheritance) relationship.
3 In case of method overloading, parameter must be In case of method overriding, parameter must be
different. same.
4 Method overloading is the example of compile time Method overriding is the example of run time
polymorphism. polymorphism.
5 In java, method overloading can't be performed by Return type must be same or covariant in method
changing return type of the method only. Return type overriding.
can be same or different in method overloading. But
you must have to change the parameter.
super Keyword in Java Inheritance
Previously we saw that the same method in the subclass overrides the method in
superclass.
In such a situation, the super keyword is used to call the method of the parent
class from the method of the child class.
You can call the superclass's method from within the subclass using
super.methodName().
Additionally, super() is used to call the constructor of the superclass from the
subclass constructor, which is essential for initializing inherited members.
Using super to Call Superclass
Constructors
A subclass can call a constructor defined by its superclass by use of the following
form of super:
super(arg-list);
Here, arg-list specifies any arguments needed by the constructor in the superclass.
super( ) must always be the first statement executed inside a subclass’ constructor.
A Second Use for super
The second form of super acts somewhat like this, except that it always refers to
the superclass of the subclass in which it is used. This usage has the following
general form:
super.member
In Java, it is possible to inherit attributes and methods from one class to another.
We group the "inheritance concept" into two categories: