» » » Why Java does not supports multiple inheritance?

Inheritance:

  • The concept of getting properties of one class object to another class object is known as inheritance.
  • Here properties means variable and methods.

Types of Inheritance:

  1. Multiple inheritance.
  2. Multilevel inheritance.

 Multiple inheritance:


  • The concept of Getting the properties from multiple class objects to sub class object with same priorities is known as multiple inheritance.
  • Java Doesn't Support multiple Inheritance.

Diamond problem:

  • In multiple inheritance there is every chance of multiple properties of multiple objects with  the same name available to the sub class object with same priorities leads for the ambiguity.

  1. //Multiple inheritance program
  2. Class A{
  3. }
  4. Class B extends A{
  5. public void show(){
  6. }
  7. }
  8. Class C extends A{
  9. public void show(){
  10. }
  11. }
  12. Class D extends B,C{  // not supported by java leads to syntax error.
  13. }


Why multiple inheritanc eis not possible in java




  •  We have two classes B and c which are inheriting A class properties.
  • Here Class D inheriting B class and C class So properties present in those classes will be available in java.
  • But both classes are in same level with same priority.
  • If we want to use show() method that leads to ambiguity
  • This is called diamond problem.
  • Because of multiple inheritance there is chance of the root object getting created more than once.
  • Always the root object i.e object of object class hast to be created only once.
  • Because of above mentioned reasons multiple inheritance would not be supported by java.
  • Thus in java a class can not extend more than one class simultaneously. At most a class can extend only one class.
So these are the reasons that java does not supports multiple inheritance.Even though having this much reasons some people say and we also gets some doubts when we are using interfaces. lets me clear this one also. our immediate question should be.

Is Java supports multiple inheritance using interfaces?

  • Before that we need to know about interfaces.
  • Interfaces having fully abstract functionality.
  • Means methods in interfaces by default public abstract methods so we need to implement these methods in classes which are extending this interface.
   
  1. /Multiple inheritance program
  2. interface A{
  3. public void  show();
  4. }
  5. interface B{
  6. public void  display();
  7. }
  8. Class C Implements A,B{
  9. }



  • Here it seems we are achieving multiple inheritance by using interfaces. but we are not.
  • Syntactically it seems to multiple inheritance but the actual implementation of multiple inheritance is not there. how can i say that? let me clear

Difference between interfaces and inheritance: in multiple inheritance

  • Inheritance means getting the properties from one class object to another class object.
  • Means if any class extending another class then the super class methods can be used in sub classes happily.
  • But in interfaces the methods in interfaces are fully abstract so we need to provide functionality in our extended class and use it .seems both are opposite right? yes.
  • That is the reason we can say interfaces are only supports syntactical multiple inheritance which is not implementation of multiple inheritance.
So finally java supports only syntax of multiple inheritance does not supports implementation of multiple inheritance.
Inheritance is like debit and interface is like credit but interface has its own importance in other concepts like server side programming

What is the major change in JAVA 8 regarding interfaces:

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

3 comments for Why Java does not supports multiple inheritance?

  1. Thank you.im am fresher in programing field.and little nerves to learn programs.and i saw this website.its all about easy to learn java.

    ReplyDelete
    Replies
    1. yeah .. vikky your correct !!!

      Delete
    2. Wow :o Awesome !!!
      Explanation ,I never seen any website so far rather than it ...it cleared my doubts whichever I was getting on OOPS as well Collections ...

      Really I would completely appreciate from my deep heart

      Delete

Select Menu