Example of multiple inheritance in Java and python
- Details
- Category: Uncategorised
- Published: Sunday, 04 May 2025 04:29
- Written by Super User
- Hits: 55
Note: Java does not support multiple inheritance of classes to avoid complexities (like the diamond problem). However, you can achieve similar behavior by using interfaces. A Java class can implement multiple interfaces, which allows you to combine behaviors from different sources.
// Define the first interface with its own abstract method
interface InterfaceA {
void methodA();
}
// Define the second interface with another abstract method
interface InterfaceB {
void methodB();
}
// A class that "inherits" from both interfaces by implementing them
public class MultipleInheritanceDemo implements InterfaceA, InterfaceB {
// Provide implementation for method from InterfaceA
public void methodA() {
System.out.println("Implementation of methodA from InterfaceA");
}
// Provide implementation for method from InterfaceB
public void methodB() {
System.out.println("Implementation of methodB from InterfaceB");
}
public static void main(String[] args) {
MultipleInheritanceDemo obj = new MultipleInheritanceDemo();
obj.methodA(); // Calls implementation from InterfaceA
obj.methodB(); // Calls implementation from InterfaceB
}
}
Explanation:
-
Interfaces Defined: Two interfaces,
InterfaceA
andInterfaceB
, are defined, each declaring a method (methodA
andmethodB
, respectively). -
Implementing Multiple Interfaces: The class
MultipleInheritanceDemo
implements both interfaces, thus inheriting the abstract methods and providing concrete implementations. -
Behavior: When an instance of
MultipleInheritanceDemo
is created, it can use methods from both interfaces, effectively mimicking multiple inheritance by bundling behaviors from multiple sources.
Multiple Inheritance in Python
Python natively supports multiple inheritance, allowing a class to directly inherit from more than one parent class. This can be a very powerful feature, though it also requires careful design to avoid conflicts
# First parent class with its own method
class Parent1:
def method1(self):
print("Method from Parent1")
# Second parent class with another method
class Parent2:
def method2(self):
print("Method from Parent2")
# Child class that inherits from both Parent1 and Parent2
class Child(Parent1, Parent2):
def method_child(self):
print("Method from Child")
# Using the Child class
child = Child()
child.method1() # Inherited from Parent1
child.method2() # Inherited from Parent2
child.method_child() # Own method
Explanation:
-
Multiple Parent Classes:
Parent1
andParent2
each provide their own methods (method1
andmethod2
). -
Child Inheriting Multiple Classes: The
Child
class inherits from bothParent1
andParent2
. This means it automatically has access to bothmethod1
(fromParent1
) andmethod2
(fromParent2
), in addition to its ownmethod_child
. -
Method Resolution Order (MRO): Python uses a specific algorithm (C3 linearization) to decide which method to execute when there is a potential conflict. In this simple example, there is no conflict.
-
Runtime Behavior: When an instance of
Child
callsmethod1()
ormethod2()
, Python dynamically determines which method to execute based on the class hierarchy.
Both examples illustrate how multiple inheritance can be achieved in different programming languages:
-
Java: Uses interfaces to simulate multiple inheritance since a class can implement many interfaces.
-
Python: Supports multiple inheritance directly through its class declaration syntax.