INHERITANCE
WHAT IS INHERITANCE?
In Java, Inheritance means creating new classes
based on existing ones. A class that inherits from
another class can reuse the methods and fields of
that class. In addition, you can add new fields and
methods to your current class as well.
WHY DO WE NEED JAVA INHERITANCE?
Code Reusability: The code written in the Superclass is
common to all subclasses. Child classes can directly use the
parent class code.
Method Overriding: Method Overriding is achievable only
through Inheritance. It is one of the ways by which Java achieves
Run Time Polymorphism.
Abstraction: The concept of abstract where we do not have to
provide all details, is achieved through
inheritance. Abstraction only shows the functionality to the user.
// Driver Class
// Java Program to illustrate Inheritance (concise) class Gfg {
public static void main(String args[])
import java.io.*; {
Engineer E1 = new Engineer();
// Base or Super Class
System.out.println("Salary : " +
class Employee {
E1.salary
int salary = 60000;
+ "\nBenefits : " +
}
E1.benefits);
}
// Inherited or Sub Class
class Engineer extends Employee { } OUTPUT
int benefits = 10000;
}
TYPES OF INHERITANCE
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Multiple Inheritance
Hybrid Inheritance
SINGLE INHERITANCE
In single inheritance, a sub-class is derived
from only one super class. It inherits the properties and
behavior of a single-parent class. Sometimes, it is also known
as simple inheritance. In the below figure, ‘A’ is a parent class
and ‘B’ is a child class. The class ‘B’ inherits all the properties
of the class ‘A’.
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. In the image below,
Class C is derived from interfaces A and B.
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.
ADVANTAGES OF INHERITANCE IN JAVA
Code Reusability
Abstraction
Polymorphism
DISADVANTAGE
Complexity
Tight Coupling