Inheritance
• inheritance is a fundamental object-oriented
programming concept that allows one class
(called the subclass or child class) to inherit
the properties (fields) and behaviors
(methods) of another class (called the
superclass or parent class).
• This helps to achieve reusability and establish
a relationship between different classes.
Key Concepts of Inheritance in Java:
• Superclass (Parent Class): The class whose
properties and methods are inherited by another
class.
• Subclass (Child Class): The class that inherits the
properties and methods from another class.
• extends keyword: Used to declare inheritance in
Java. A subclass extends a superclass.
• 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, 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.