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

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top