Sunday, 11 August 2019

Why multiple inheritance not support in Java

In languages like C++ multiple inheritance play a important role to implement many of applications. In multiple inheritance we inherit the properties and behaviors of more than one class into a single class. For example

In this diagram class C inherits the properties and behaviors of class A and class B. This type of implementation is called " multiple inheritance" and it is not supported in java using classes. But we implement multiple inheritance using classes.


Why multiple inheritance is not directly supported in Java?

To reduce the complexity and simplify the language. multiple inheritance is not supported in java.

Consider a scenario where A and B are super classes and C is derived class derived from A and B like below

Since both super classes contains same method and you need to call those two methods using sub class object. that class C object.

Since compile time errors are better than run time errors, java renders compile time error if you inherit two classes in the above form. so whether you have same method or different there will be a compile time error.

For example consider the java program

class A
{
 void display()
 {
  System.out.println(" hellp");
 }
}
class B
{
 void display()
 {
  System.out.println("world");
 }
}

class C extend A,B
{
 public static void main(String args[])
 {
  C c1=new C();
  c1.display();
}
}
The above program generated the following output


 The error is at line " class C extend A,B" where we try to inherit two class.

0 comments:

Post a Comment

Scribe to Youtube Channel

Advertisement

Find Us On Facebook

C Programming

More

C++ Tutorial

More

Java Tutorial

More

software engineering

More

MS Office

More

Database Management

More
Top