implementing multilevel inheritance in java and python
- Details
- Category: Uncategorised
- Published: Sunday, 04 May 2025 04:08
- Written by Super User
- Hits: 57
// Base class (Grandparent)
class Grandparent {
Grandparent() {
System.out.println("Grandparent class constructor called");
}
void grandparentMethod() {
System.out.println("Method from Grandparent class");
}
}
// Intermediate class (Parent) inheriting from Grandparent
class Parent extends Grandparent {
Parent() {
System.out.println("Parent class constructor called");
}
void parentMethod() {
System.out.println("Method from Parent class");
}
}
// Derived class (Child) inheriting from Parent
class Child extends Parent {
Child() {
System.out.println("Child class constructor called");
}
void childMethod() {
System.out.println("Method from Child class");
}
}
// Main class to execute the program
public class MultilevelInheritanceDemo {
public static void main(String[] args) {
// Creating an instance of Child class
Child childObj = new Child();
// Calling methods from all levels
childObj.grandparentMethod();
childObj.parentMethod();
childObj.childMethod();
}
}
### Explanation:
1. **Grandparent Class:** Contains a constructor and method.
2. **Parent Class:** Inherits from `Grandparent` and adds its own constructor and method.
3. **Child Class:** Inherits from `Parent` and introduces an additional constructor and method.
4. **Execution:** When a `Child` object is instantiated, constructors from all parent classes are invoked due to the inheritance chain.
5. **Method Accessibility:** The `Child` object can call methods from `Parent` and `Grandparent`, demonstrating **multilevel inheritance**.
This will output:
Grandparent class constructor called
Parent class constructor called
Child class constructor called
Method from Grandparent class
Method from Parent class
Method from Child class
# Base class
class Grandparent:
def __init__(self):
print("Grandparent class constructor called")
def grandparent_method(self):
print("Method from Grandparent class")
# Intermediate class inheriting from Grandparent
class Parent(Grandparent):
def __init__(self):
super().__init__()
print("Parent class constructor called")
def parent_method(self):
print("Method from Parent class")
# Derived class inheriting from Parent
class Child(Parent):
def __init__(self):
super().__init__()
print("Child class constructor called")
def child_method(self):
print("Method from Child class")
# Creating an object of Child class
child_obj = Child()
# Calling methods from all levels of inheritance
child_obj.grandparent_method()
child_obj.parent_method()
child_obj.child_method()
Explanation:
-
Grandparent Class: The base class with a constructor and method.
-
Parent Class: Inherits from
Grandparent
and has its own constructor and method. -
Child Class: Inherits from
Parent
and has its own constructor and method. -
Calling Methods: The
Child
class object can access methods from all classes in the hierarchy using inheritance.
When executed, the program will display messages from all constructors and allow the child object to call methods from its parent and grandparent.