Multiple Inheritance in Java

All other Source.Python topics and issues.
akshaysharma12
Junior Member
Posts: 6
Joined: Tue Jul 12, 2022 6:29 am

Multiple Inheritance in Java

Postby akshaysharma12 » Tue Aug 02, 2022 6:55 am

What are the types of Inheritance?
akshaysharma12
Junior Member
Posts: 6
Joined: Tue Jul 12, 2022 6:29 am

Re: Multiple Inheritance in Java

Postby akshaysharma12 » Wed Oct 12, 2022 9:35 am

Inheritance is the most important pillar of object-oriented programming language. It is the property in which the main class inherits the methods of its parent class. There are different types of inheritance named as single inheritance, multi-level inheritance, hierarchical inheritance, multiple inheritance, and hybrid inheritance.

Multiple inheritance in java is the process in which the base class extends more than one class. The derived class will inherit all the properties and methods of the parent class.

public class Main
{
public static class Test1{
void method(){
System.out.println("I am in Test1");
}
}
public static class Test2 {
void method(){
System.out.println("I am in Test2");
}
}
public static class Test3 extends Test1{

}

public static void main(String[] args) {
System.out.println("Hello World");
Test3 obj = new Test3();
obj.method();
}
}

Output
Compile time error

It shows you compile time error. It shows this error because the compiler is confused about which method function to call, whether the method of Test1 or the method of Test2. The compile time error is better than the run time error. Java does not support multiple inheritance, which means one class cannot extend more than one class because of the ambiguity error, i.e., a diamond problem. It happens when the derived class extends two base classes with the method with the same name, and the same parameters.Java solves this using interfaces. It makes it easy to use and deal with ambiguity issues. Interfaces in java are used to implement the properties and methods of two classes simultaneously.
It helps achieve the loose coupling of classes, and in this way, multiple inheritance is implemented in java.
Let’s understand this through the below program.

public class Main
{
interface Test1{
default void method(){
System.out.println("I am in Test1");
}
}
interface Test2 {
default void method(){
System.out.println("I am in Test2");
}
}
public static class Test3 implements Test1,Test2{

public void method(){
Test1.super.method();
//This will call the method of Test1
Test2.super.method();
//This will call the method of Test2
}
}

public static void main(String[] args) {
Test3 obj = new Test3();
obj.method();
}
}
Output
I am in Test1
I am in Test2

We can see that using the interface lets the compiler know which method to call when. The interface helps in achieving multiple inheritance in java. Through the interface, you can implement two classes at a single time.
You can also use abstract methods in the interface for achieving multiple inheritance.


public class Main
{
interface Test1{
public abstract void method1();

}
interface Test2 {
public abstract void method2();
}
public static class Test3 implements Test1,Test2{
//Abstract methods are written
public void method1(){
System.out.prinltn(“Coding”);
}
Public void method2(){
System.out.prinltn(“Ninjas”);

}
}

public static void main(String[] args) {
Test3 obj = new Test3();
obj.method1();
obj.method2();
}
}
Output
Coding
Ninjas
In this, we have taken abstract method methd1 and method2 in the interfaces, and these methods are defined in class test3. In this way, you have overridden the methods and achieved multiple inheritance.
User avatar
Articha
Member
Posts: 32
Joined: Tue Sep 21, 2021 12:13 pm
Location: Russia

Postby Articha » Thu Oct 13, 2022 8:47 pm

No multiple inheritance in java. After "extends" u can choose only one class

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 33 guests