0% found this document useful (0 votes)
19 views

Multilevel Inheritance in Java

Uploaded by

Shashank shekhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Multilevel Inheritance in Java

Uploaded by

Shashank shekhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

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:

The base (parent) class contains common attributes and methods.


The first child class extends the base class and adds additional functionality.
A second child class further extends the first child class, inheriting its features and potentially
adding new ones.

This forms a chain-like hierarchy where each class inherits from its immediate parent.

Syntax of Multilevel Inheritance

Here’s a simple example of multilevel inheritance in Java:

java

// Parent class
class GrandParent {
void displayGrandParent() {
System.out.println("This is the Grandparent class.");
}
}

// Child class inheriting from GrandParent


class Parent extends GrandParent {
void displayParent() {
System.out.println("This is the Parent class.");
}
}

// Child class inheriting from Parent


class Child extends Parent {
void displayChild() {
System.out.println("This is the Child class.");
}
}

public class MultilevelInheritanceDemo {


public static void main(String[] args) {
// Creating an object of the Child class
Child obj = new Child();

// Calling methods from all classes


obj.displayGrandParent(); // Method from GrandParent
obj.displayParent(); // Method from Parent
obj.displayChild(); // Method from Child

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/7
}
}

Output

csharp

This is the Grandparent class.


This is the Parent class.
This is the Child class.

Explanation

1. `GrandParent` is the base class (superclass).


2. `Parent` extends `GrandParent` and inherits its method `displayGrandParent()`.
3. `Child` extends `Parent` and inherits methods from both `GrandParent` and `Parent`.
4. When we create an object of the `Child` class, it can access methods from all three levels of the
hierarchy.

Key Points About Multilevel Inheritance

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

`GrandParent` class → Represents the foundation or root.


`Parent` class → Adds some specific features.
`Child` class → Combines all inherited features and adds its own.

For example:

Grandfather → Father → Child.

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.

There are five types of inheritance in Java:

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.");
}
}

public class SingleInheritance {


public static void main(String[] args) {
Child c = new Child();
c.display(); // Method from Parent class
c.show(); // Method from Child class
}
}

Output

csharp

This is the Parent class.

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.");
}
}

public class MultilevelInheritance {


public static void main(String[] args) {
Child c = new Child();
c.methodGrandParent(); // Grandparent class method
c.methodParent(); // Parent class method
c.methodChild(); // Child class method
}
}

Output

csharp

This is the Grandparent class.


This is the Parent class.
This is the Child class.

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.");
}
}

public class HierarchicalInheritance {


public static void main(String[] args) {
Child1 c1 = new Child1();
Child2 c2 = new Child2();

c1.display(); // Method from Parent class


c1.showChild1(); // Method from Child1 class

c2.display(); // Method from Parent class


c2.showChild2(); // Method from Child2 class
}
}

Output

csharp

This is the Parent class.


This is Child1 class.
This is the Parent class.
This is Child2 class.

4. Multiple Inheritance (Through Interfaces)


Java does not support multiple inheritance with classes to avoid ambiguity. However, it is achieved
using interfaces.

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();
}

// Child class implementing multiple interfaces


class Child implements Interface1, Interface2 {
public void method1() {
System.out.println("Method1 from Interface1");
}

public void method2() {


System.out.println("Method2 from Interface2");
}
}

public class MultipleInheritance {


public static void main(String[] args) {
Child c = new Child();
c.method1(); // Interface1 method
c.method2(); // Interface2 method
}
}

Output

csharp

Method1 from Interface1


Method2 from Interface2

5. Hybrid Inheritance (Through Interfaces)


Hybrid inheritance is a mix of two or more types of inheritance (e.g., hierarchical + multiple). In Java, this
is achieved through interfaces.

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.");
}
}

// Child Class implementing multiple interfaces


class Child extends Parent implements Interface1, Interface2 {
public void method1() {
System.out.println("Method1 from Interface1");
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/7
}

public void method2() {


System.out.println("Method2 from Interface2");
}
}

public class HybridInheritance {


public static void main(String[] args) {
Child c = new Child();
c.display(); // Parent class method
c.method1(); // Interface1 method
c.method2(); // Interface2 method
}
}

Output

csharp

This is the Parent class.


Method1 from Interface1
Method2 from Interface2

Summary Table
Type of Inheritance Description Example

Single Inheritance One class inherits from another class. Parent → Child

Multilevel A class inherits from a child class, forming a


GrandParent → Parent → Child
Inheritance chain.

Hierarchical Multiple child classes inherit from one parent


Parent → Child1, Child2
Inheritance class.

Multiple Inheritance Achieved through interfaces in Java. Class implements Interface1,2


Combination of multiple and hierarchical Class extends Parent, implements
Hybrid Inheritance
inheritance. Interfaces

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

You might also like