Multilevel Inheritance in Java
Multilevel Inheritance in Java
Multilevel inheritance in Java is a type of inheritance where a class is derived from a child class, making it
part of a hierarchy. Essentially, a class acts as a parent (superclass) for another class, and the derived
class itself becomes a parent for yet another class.
In multilevel inheritance:
This forms a chain-like hierarchy where each class inherits from its immediate parent.
java
// Parent class
class GrandParent {
void displayGrandParent() {
System.out.println("This is the Grandparent class.");
}
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/7
}
}
Output
csharp
Explanation
1. Hierarchy: Forms a chain where each class inherits from its immediate superclass.
2. Code Reusability: Promotes code reusability by allowing derived classes to reuse features of
parent classes.
3. Accessing Parent Features: A child class can directly call methods and attributes inherited from its
parent classes.
4. Object Creation: An object of the lowest class (child) can access methods of all its parent classes.
Real-World Analogy
For example:
Key Considerations
Single Inheritance Only: Java does not support multiple inheritance (a class inheriting from two
classes) due to ambiguity problems.
Object Class: All classes in Java indirectly inherit from the `Object` class at the topmost level.
Let me know if you'd like a deeper dive into real-world examples or how multilevel inheritance compares
to other inheritance types!
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/7
Explain different type of inheritance in JAVA along with sample programs.
In Java, inheritance is the mechanism by which one class can acquire properties (fields) and behaviors
(methods) of another class. It enhances code reusability and establishes relationships between classes.
1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance
4. Multiple Inheritance (through Interfaces)
5. Hybrid Inheritance (through Interfaces)
1. Single Inheritance
In single inheritance, a child class inherits from a single parent class. It allows a class to reuse methods
and fields of the parent class.
Program Example
java
// Parent Class
class Parent {
void display() {
System.out.println("This is the Parent class.");
}
}
// Child Class
class Child extends Parent {
void show() {
System.out.println("This is the Child class.");
}
}
Output
csharp
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/7
This is the Child class.
2. Multilevel Inheritance
In multilevel inheritance, a class inherits from a child class, making it part of a chain of inheritance.
Program Example
java
// Grandparent Class
class GrandParent {
void methodGrandParent() {
System.out.println("This is the Grandparent class.");
}
}
// Parent Class
class Parent extends GrandParent {
void methodParent() {
System.out.println("This is the Parent class.");
}
}
// Child Class
class Child extends Parent {
void methodChild() {
System.out.println("This is the Child class.");
}
}
Output
csharp
3. Hierarchical Inheritance
In hierarchical inheritance, multiple child classes inherit from the same parent class.
Program Example
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/7
java
// Parent Class
class Parent {
void display() {
System.out.println("This is the Parent class.");
}
}
// Child1 Class
class Child1 extends Parent {
void showChild1() {
System.out.println("This is Child1 class.");
}
}
// Child2 Class
class Child2 extends Parent {
void showChild2() {
System.out.println("This is Child2 class.");
}
}
Output
csharp
Program Example
java
// Interface1
interface Interface1 {
void method1();
}
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/7
// Interface2
interface Interface2 {
void method2();
}
Output
csharp
Program Example
java
// Interface1
interface Interface1 {
void method1();
}
// Interface2
interface Interface2 {
void method2();
}
// Parent Class
class Parent {
void display() {
System.out.println("This is the Parent class.");
}
}
Output
csharp
Summary Table
Type of Inheritance Description Example
Single Inheritance One class inherits from another class. Parent → Child
Java does not allow multiple inheritance with classes but allows it with interfaces to avoid ambiguity.
If you want a deeper explanation of any specific type, feel free to ask! 😊
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/7