Inheritance In Java
Inheritance in Java allows a new class (subclass or child class) to inherit properties
and behaviors (fields and methods) from an existing class (superclass or parent
class). This helps in reusing code and establishing a relationship between classes.
Types of Inheritance in Java:
Single Inheritance
One class inherits from another class.
Example: A class "Staff" inherits from "HOD".
Multilevel Inheritance
A class inherits from a subclass, making a chain of inheritance.
Example: "AssistantStaff" inherits from "Staff", and "Staff" inherits from "HOD".
Hierarchical Inheritance
Multiple classes inherit from a single superclass.
Example: "TeachingStaff" and "NonTeachingStaff" both inherit from "HOD".
Hybrid Inheritance
A combination of two or more types of inheritance. (Not supported directly in
Java, but can be implemented through interfaces).
Example: Combination of hierarchical and multilevel inheritance.
1. Single Inheritance
Definition: In Single Inheritance, a child class inherits properties and methods of
one parent class.
Example:
class HOD {
void manageDepartment() {
System.out.println("Managing the department.");
class Staff extends HOD {
void workOnAssignments() {
System.out.println("Working on assignments.");
public class Main {
public static void main(String[] args) {
Staff staff = new Staff();
staff.manageDepartment(); // Inherited from HOD
staff.workOnAssignments(); // Specific to Staff
Diagram:
HOD
Staff
2. Multilevel Inheritance
Definition: In Multilevel Inheritance, a class is derived from a subclass, creating a
multi-level hierarchy.
Example:
class HOD {
void manageDepartment() {
System.out.println("Managing the department.");
class Staff extends HOD {
void workOnAssignments() {
System.out.println("Working on assignments.");
class AssistantStaff extends Staff {
void assistInTeaching() {
System.out.println("Assisting in teaching.");
}
public class Main {
public static void main(String[] args) {
AssistantStaff assistantStaff = new AssistantStaff();
assistantStaff.manageDepartment(); // Inherited from HOD
assistantStaff.workOnAssignments(); // Inherited from Staff
assistantStaff.assistInTeaching(); // Specific to AssistantStaff
Diagram:
HOD
Staff
AssistantStaff
3. Hierarchical Inheritance
Definition: In Hierarchical Inheritance, multiple classes inherit from a single
superclass.
Example:
class HOD {
void manageDepartment() {
System.out.println("Managing the department.");
}
class TeachingStaff extends HOD {
void teachSubjects() {
System.out.println("Teaching subjects.");
class NonTeachingStaff extends HOD {
void manageAdministration() {
System.out.println("Managing administration tasks.");
public class Main {
public static void main(String[] args) {
TeachingStaff teacher = new TeachingStaff();
teacher.manageDepartment(); // Inherited from HOD
teacher.teachSubjects(); // Specific to TeachingStaff
NonTeachingStaff nonTeacher = new NonTeachingStaff();
nonTeacher.manageDepartment(); // Inherited from HOD
nonTeacher.manageAdministration(); // Specific to NonTeachingStaff
Diagram:
HOD
/ \
TeachingStaff NonTeachingStaff
4. Hybrid Inheritance
Definition: Hybrid inheritance is a combination of two or more types of
inheritance. Java doesn't support this directly through classes but can be
implemented using interfaces.
Example (Using Interfaces for Hybrid):
interface HOD {
void manageDepartment();
interface Staff {
void workOnAssignments();
class AssistantStaff implements HOD, Staff {
public void manageDepartment() {
System.out.println("Managing the department.");
public void workOnAssignments() {
System.out.println("Working on assignments.");
public class Main {
public static void main(String[] args) {
AssistantStaff assistantStaff = new AssistantStaff();
assistantStaff.manageDepartment(); // From HOD interface
assistantStaff.workOnAssignments(); // From Staff interface
Diagram:
HOD Staff
\ /
AssistantStaff
Step-by-Step Explanation
Start with a Simple Example:
Begin with the HOD class, which represents the head of the department. The HOD
has a method that shows their responsibility, like managing the department.
Introduce Staff Class:
The Staff class represents the staff members working in the department. The staff
inherits from HOD, meaning it can use the manageDepartment method from HOD,
but it also has additional responsibilities, like working on assignments.
Use Multiple Classes for Multilevel Inheritance:
The AssistantStaff class is an example of multilevel inheritance. This class inherits
from Staff, so it can perform both tasks: managing the department and working on
assignments. It also has its own special responsibility, such as assisting in teaching.
Demonstrate Hierarchical Inheritance
In hierarchical inheritance, multiple subclasses (like TeachingStaff and
NonTeachingStaff) inherit from the same superclass (HOD), but they perform
different tasks. The teacher teaches subjects, while the non-teaching staff handles
administration.
Explain Hybrid Inheritance via Interfaces:
To demonstrate hybrid inheritance, use interfaces. AssistantStaff implements both
the HOD and Staff interfaces, allowing it to take up responsibilities from both.