Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Why Java Doesn’t Support Multiple Inheritance

  1. #1
    Junior Member
    Join Date
    Jul 2018
    Location
    Mumbai, India
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why Java Doesn’t Support Multiple Inheritance

    The main point to be discussed over here is Java does not support multiple inheritance or Java does support? There are lots of people who are still under confusion that it supports but actually it does not support using interface in Java. Even the father of Java has once admitted the fact.

  2. #2
    Junior Member
    Join Date
    Aug 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why Java Doesn’t Support Multiple Inheritance

    Hello,

    Quick answer is NO, Java dose not support multiple inheritance.

    Here is why it dose not support:

    Lets consider the following class hierarchy:

    public class Car {
         public void accelerate() {}
    }
     
    public class SportsCar extends Car {
         public void accelerate() {} //<----- overridden inherited method from Car class  
    }
     
    public class SUV extends Car {
         public void accelerate() {} //<----- overridden inherited method from Car class
    }
     
    public class Mercedes extends SportsCar, SUV {
         public void accelerate() {} //<----- Mercedes class inherit the accelerate method but the question is WHICH ONE???
    }

    That is why Java do not allow multiple inheritance. This is also called the "Deadly Diamond of Death"

    I hope this helps.
    Regards
    Alin

  3. #3
    Member
    Join Date
    Sep 2018
    Posts
    32
    Thanks
    0
    Thanked 9 Times in 6 Posts

    Default Re: Why Java Doesn’t Support Multiple Inheritance

    C++ , Common lisp and few other languages supports multiple inheritance while java doesn’t support it. Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it. One of the example of such problem is the diamond problem that occurs in multiple inheritance.

    To understand the basics of inheritance, refer this main guide: Inheritance in Java

    What is diamond problem?
    We will discuss this problem with the help of the diagram below: which shows multiple inheritance as Class D extends both classes B & C. Now lets assume we have a method in class A and class B & C overrides that method in their own way. Wait!! here the problem comes – Because D is extending both B & C so if D wants to use the same method which method would be called (the overridden method of B or the overridden method of C). Ambiguity. That’s the main reason why Java doesn’t support multiple inheritance.
    Diamond Problem

    Can we implement more than one interfaces in a class
    Yes, we can implement more than one interfaces in our program because that doesn’t cause any ambiguity(see the explanation below).

    interface X
    {
    public void myMethod();
    }
    interface Y
    {
    public void myMethod();
    }
    class JavaExample implements X, Y
    {
    public void myMethod()
    {
    System.out.println("Implementing more than one interfaces");
    }
    public static void main(String args[]){
    JavaExample obj = new JavaExample();
    obj.myMethod();
    }
    }
    Output:

    Implementing more than one interfaces
    As you can see that the class implemented two interfaces. A class can implement any number of interfaces. In this case there is no ambiguity even though both the interfaces are having same method. Why? Because methods in an interface are always abstract by default, which doesn’t let them give their implementation (or method definition ) in interface itself.

  4. #4
    Member
    Join Date
    Sep 2018
    Posts
    32
    Thanks
    0
    Thanked 9 Times in 6 Posts

    Default Re: Why Java Doesn’t Support Multiple Inheritance

    C++ , Common lisp and few other languages supports multiple inheritance while java doesn’t support it. Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it. One of the example of such problem is the diamond problem that occurs in multiple inheritance.

    To understand the basics of inheritance, refer this main guide: Inheritance in Java

    What is diamond problem?
    We will discuss this problem with the help of the diagram below: which shows multiple inheritance as Class D extends both classes B & C. Now lets assume we have a method in class A and class B & C overrides that method in their own way. Wait!! here the problem comes – Because D is extending both B & C so if D wants to use the same method which method would be called (the overridden method of B or the overridden method of C). Ambiguity. That’s the main reason why Java doesn’t support multiple inheritance.
    Diamond Problem

    Can we implement more than one interfaces in a class
    Yes, we can implement more than one interfaces in our program because that doesn’t cause any ambiguity(see the explanation below).

    interface X
    {
    public void myMethod();
    }
    interface Y
    {
    public void myMethod();
    }
    class JavaExample implements X, Y
    {
    public void myMethod()
    {
    System.out.println("Implementing more than one interfaces");
    }
    public static void main(String args[]){
    JavaExample obj = new JavaExample();
    obj.myMethod();
    }
    }
    Output:

    Implementing more than one interfaces
    As you can see that the class implemented two interfaces. A class can implement any number of interfaces. In this case there is no ambiguity even though both the interfaces are having same method. Why? Because methods in an interface are always abstract by default, which doesn’t let them give their implementation (or method definition ) in interface itself.

  5. #5
    Junior Member
    Join Date
    Oct 2018
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why Java Doesn’t Support Multiple Inheritance

    This is trying to get closer to multiple inheritance. What we do is implement multiple interface, here we are not extending (inheriting) anything.
    The implementing class is the one that is going to add the properties and behavior. It is not getting the implementation free from the parent classes. I would simply say, there is no support for multiple inheritance in java.

  6. #6

    Default Re: Why Java Doesn’t Support Multiple Inheritance

    Multiple Inheritance is a feature of object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when there exist methods with the same signature in both the super classes and subclass. On calling the method, the compiler cannot determine which class method to be called and even on calling which class method gets the priority.

  7. #7
    Member
    Join Date
    Sep 2018
    Posts
    32
    Thanks
    0
    Thanked 9 Times in 6 Posts

    Default Re: Why Java Doesn’t Support Multiple Inheritance

    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.
    C++ , Common lisp and few other languages supports multiple inheritance while java doesn’t support it. Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it.
    Yes, we can implement more than one interfaces in our program because that doesn’t cause any ambiguity(see the explanation below).

    interface X
    {
    public void myMethod();
    }
    interface Y
    {
    public void myMethod();
    }
    class JavaExample implements X, Y
    {
    public void myMethod()
    {
    System.out.println("Implementing more than one interfaces");
    }
    public static void main(String args[]){
    JavaExample obj = new JavaExample();
    obj.myMethod();
    }
    }
    Output:

    Implementing more than one interfaces
    As you can see that the class implemented two interfaces. A class can implement any number of interfaces. In this case there is no ambiguity even though both the interfaces are having same method. Why? Because methods in an interface are always abstract by default, which doesn’t let them give their implementation (or method definition ) in interface itself.

  8. #8
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Why Java Doesn’t Support Multiple Inheritance

    Questions like this are best googled. Too many folks contributing at times conflicting answers or opinions tends to cloud the issue.

    https://www.javaworld.com/article/20...heritance.html

    Regards,
    Jim

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why Java Doesn’t Support Multiple Inheritance

    Java doesn’t allow multiple inheritance.
    That does not answer the OPs question.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Why Java Doesn’t Support Multiple Inheritance

    Quote Originally Posted by meenal View Post

    Implementing more than one interfaces As you can see that the class implemented two interfaces. A class can implement any number of interfaces. In this case there is no ambiguity even though both the interfaces are having same method. Why? Because methods in an interface are always abstract by default, which doesn’t let them give their implementation (or method definition ) in interface itself.
    Not quite correct. Try implementing these interfaces in the same class.

    interface Foo {
       double method(int a);
    }
     
    interface Bar {
       int method(int a);
    }

    Regards,
    Jim

  11. #11

    Default Re: Why Java Doesn’t Support Multiple Inheritance

    Because multiple inheritance makes resolving access to data at compile time much more complex.

    Traditional compilers transform access to static variable (by name) to access to data at given offset from the address of the object (or structure).

    Regular inheritance add instance variables further away from the object address, so all compiled inherited methods work naturally - they still find their data at the same offsets.

    Now imagine you inherited from two classes with instance variables. They both have methods that use variable at the same offset - say the first of the instance variables. Yet only one of those instance variables can be placed at that offset.

    Multiple inheritance from data-bearing classes means the methods have to locate instance variables through some kind of reference table rather than by offsets - and that slows execution quite a bit. Not the end of the world, that's what dynamic languages like Python do - however that's not what they wanted from Java.

Similar Threads

  1. Confusion in Multiple inheritance in Java
    By Ganeprog in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 9th, 2014, 09:35 AM
  2. why java does not support multiple inheritance?
    By venkatsairam in forum Object Oriented Programming
    Replies: 3
    Last Post: January 18th, 2012, 05:25 PM
  3. Replies: 1
    Last Post: September 30th, 2010, 02:36 PM