Home » Java programming language

Why java does not support multiple inheritances?

In this article, we are going to learn about one of the java’s inheritance features - why it does not support multiple inheritances?
Submitted by Preeti Jain, on February 18, 2018

First we will understand what is inheritance?

Inheritance means when a method is defined once and it can use anywhere without rewriting again. With the help of inheritance. We can achieve reusability.

What is multiple inheritance?

Inheritance is a concept which is applicable in most of the programming language. When class A is extendable by class B and a class B is extendable by class C.

Syntax

interface A{

	public void a(){

	}
}

interface B extends A{

	public void a(){

	}
}

class C implements A,B{

	public void c(){

	}
}

In the above syntax when we will made an object of C class then with help of C class object if we call a() method with C class object then we will get ambiguity.

Java resolves ambiguity problem with the help of interface.

We cannot achieve multiple inheritance with the help of interface because interface methods need to be redefined in every child class but inheritance don't need to be redefined again that's why we cannot say interface behaves like multiple inheritance.

Example

interface A{
	public void a();
}

interface B extends A{
	public void a();
}

class InterfaceAB implements A,B{
	public void a(){
		System.out.println("Interface A a()");
	}

	public static void main(String[] args){
		InterfaceAB iab = new InterfaceAB();
		iab.a();
	}
}

Output

D:\Java Articles>java InterfaceAB
Interface A a()





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.