Why JAVA doesn't Support Multiple Inheritance with Classes?
When one class extends more than one classes then this is called multiple inheritance. For example: Class C extends class A and B then this type of inheritance is known as multiple inheritance. Java doesn’t allow multiple inheritance. In this article, we will discuss why java doesn’t allow multiple inheritance and how we can use interfaces instead of classes to achieve the same purpose.
// First Parent class
class Parent1
{
void fun()
{
System.out.println("Parent1");
}
}
// Second Parent Class
class Parent2
{
void fun()
{
System.out.println("Parent2");
}
}
// Error : Test is inheriting from multiple
// classes
class Test extends Parent1, Parent2
{
public static void main(String args[])
{
Test t = new Test();
t.fun();
}
}
t.fun()-> Compiler Do not able understand to call func() of which class. Because both Parent1,Parent2 both are having func(). Due to this ambiguity Java does not support multiple inheritance using classes.
Great work👍
ReplyDeleteDamn good mate
ReplyDeleteGood work. Nice explanation
ReplyDeleteThank u bro this question was asked to me in amazon
ReplyDeleteall doubts cleared
ReplyDelete