Assignment_9: - Program to demonstrate multiple inheritance using interfaces (Use
super keyword).
What is Multiple Inheritance?
Multiple inheritance is the process where our subclass inherits more than one superclass.
Unlike some other programming languages, like C++, Java does not support multiple
inheritance of classes. This means Java cannot inherit multiple superclasses. Java has
interfaces.
Java interfaces
Java interfaces are used to achieve abstraction, which is one of the principles in
Object-Oriented Programming. The Java interface method does not have a Java body. It is
also used to implement Multiple Interfaces in the Java language. In other words, interfaces
are the methods and variables. They cannot have a method definition or body.
Syntax of Java Interfaces.
interface
// declare constant fields
// declare methods that abstract
// by default.
}
Syntax of implementing multiple interfaces.
class MyClass implements Interface1, Interface2, Interface3
// class body
Why is Multiple Inheritance Not Supported in Java?
In Java, Multiple Inheritance is not supported by default due to several ambiguity issues.
Why this happened with Java.
Diamond Problem: One of the well-known issues in Multiple Inheritance is the
diamond problem. This problem occurs when a class inherits from two classes that
have a common method. If both parent classes have the same method with the same
name and the same parameters. Then the java interpreter can’t determine which
method to call in the child class.
Complexity: Multiple Inheritance creates more complex problems and hierarchies,
making it harder to understand and maintain code. Java focuses on keeping the
language simple and easy to learn, so it does not include features like multiple
inheritances that can increase complexity.
Ambiguity: Allowing multiple inheritance can lead to ambiguity in method
resolution. If a subclass inherits methods from two superclasses with the same
parameter but different implementations, it is unclear if implementation occurs. This
ambiguity can cause errors and make code hard to reason about.
interface A
public void addition (int num1);
interface B
public void addition (int num1);
}
class C implements A, B
public void addition (int num1)
[Link]("Output = "+num1);
public class MultpleInheritance
public static void main (String [] args)
C obj = new C ();
[Link](125);
Output
Copy
Output = 125