Why Multiple Inheritance is Not Supported in Java

In an white paper titled “Java: an Overview” by James Gosling in February 1995 gives an idea on why multiple inheritance is not supported in Java.

JAVA omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefit. This primarily consists of operator overloading (although it does have method overloading), multiple inheritance, and extensive automatic coercions.

Who better than Dr. James Gosling is qualified to make a comment on this. This paragraph gives us an overview and he touches this topic of not supporting multiple-inheritance.

Java does not support multiple inheritance

First lets nail this point. This itself is a point of discussion, whether java supports multiple inheritance or not. Some say, it supports using interface. No. There is no support for multiple inheritance in java. If you do not believe my words, read the above paragraph again and those are words of the father of Java.

This story of supporting multiple inheritance using interface is what we developers cooked up. Interface gives flexibility than concrete classes and we have option to implement multiple interface using single class. This is by agreement we are adhering to two blueprints to create a class.

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.

Multiple Inheritance

Multiple inheritance is where we inherit the properties and behavior of multiple classes to a single class. C++, Common Lisp, are some popular languages that support multiple inheritance.

Multiple Inheritance

Why Java does not support multiple inheritance?

Now we are sure that there is no support for multiple inheritance in java. But why? This is a design decision taken by the creators of java. The keyword is simplicity and rare use.

Simplicity

I want to share the definition for java given by James Gosling.

JAVA: A simple, object oriented, distributed, interpreted, robust, secure, architecture neutral, portable, high performance, multithreaded, dynamic language.

Look at the beauty of this definition for java. This should be the definition for a modern software language. What is the first characteristic in the language definition? It is simple.

Ads by Google

In order to enforce simplicity should be the main reason for omitting multiple inheritance. For instance, we can consider diamond problem of multiple inheritance.

Diamond Problem of Multiple Inheritance

We have two classes B and C inheriting from A. Assume that B and C are overriding an inherited method and they provide their own implementation. Now D inherits from both B and C doing multiple inheritance. D should inherit that overridden method, which overridden method will be used? Will it be from B or C? Here we have an ambiguity.

In C++ there is a possibility to get into this trap though it provides alternates to solve this. In java this can never occur as there is no multiple inheritance. Here even if two interfaces are going to have same method, the implementing class will have only one method and that too will be done by the implementer. Dynamic loading of classes makes the implementation of multiple inheritance difficult.

Rarely Used

We have been using java for long now. How many times have we faced a situation where we are stranded and facing the wall because of the lack of support for multiple inheritance in java? With my personal experience I don’t remember even once. Since it is rarely required, multiple inheritance can be safely omitted considering the complexity it has for implementation. It is not worth the hassle and the path of simplicity is chosen.

Even if it is required it can be substituted with alternate design. So it is possible to live without multiple inheritance without any issues and that is also one reason.

My opinion on this is, omitting support for multiple inheritance in java is not a flaw and it is good for the implementers.

This Core Java tutorial was added on 24/09/2012.

previous post: Why Android?

next post: Android Activity

234 comments on “Why Multiple Inheritance is Not Supported in Java

      • thanx for this type of exaplanation

    • It should read common lisp, not common list. Also, scala support mixin composition which is not exactly same as multiple inheritence.

      And why could not java have been designed to support multiple inheritence?

      We could say D extends C, B – make sure super calls line up properly!

      Not at all a good explaination for :

      why-multiple-inheritance-is-not-supported-in-java

      • I have fixed the typo (list to lisp), thanks for helping out.

        Scala’s support for multiple inheritance – I read online as “a flexible mixin-based composition mechanism as a clean replacement for multiple inheritance”, so I included it in the list. Looks like it can be misleading, so I have removed it now.

        I felt, I have given a clean and complete explanation for why java is not supporting multiple inheritance. If you feel otherwise, can you add your point of view to help.

        • Actually you could workaround on this limitation in Java too.
          For example external class extends one class, and it’s internal class extends another (or couple of).
          It is not exactly “multiply inheritance” but works quite similar allowing to reuse methods implementations from two or more superclasses. It sounds like Scala multiple inheritance support “a flexible mixin-based composition mechanism as a clean replacement for multiple inheritance” description fits such design. )

          • Hi Serg,

            Thanks for your comment.

      • well ofcourse super(s) can be placed in their proper positions, but the thing is people forget. it’s the same reason why servlet’s parameterised init method is depreciated not.

    • Thanks ….
      Actually i came to know the Root Cause Behind this…
      and also first time i learn the Ne term “Dimond Problem “…..

      Keep on Posting……..

      Good Explanation………

    • multiple inheretance meansone subclass and two super class
      unfortunatly two parents are not excuted in same function with different inmplementations due to sub class is getting cconfused in inheretence time hemce unable to compail

  1. nice explaanation of “Deadly diamond condition” through visual

  2. Nice expalnation.. very easy to understand

  3. Is there any work-around for situation while implementing multiple interfaces have methods with same signature?

  4. Nice Job Kulandai… !!!!! I am you fan!!!!

  5. Nice Joe.
    Can you please write blog on volatile in java with example.

  6. This article looks simple but it is an advance concept to discuss . And joe just explain it beautifully .

    Specially reference of Dr. Goslings about java.
    Thanks to contribute to community with some great articles .This is first time i am reading from a specific site regularly and subscribed .

  7. Huh, I’m confused. Everything I read said inheritance was a good thing.

    • Yes Jim, inheritance is always good, in particular for the inheritor :-)

  8. worth reading…one can easily get the concept because of diagrams used in it… thanks

  9. hi Joe,

    My opinion about interface is just a check list in java.For example if u implement any interface for a class u must implement all methods in interface u declared.

    What is the use of using interface?
    Instead of declaring it separately in interface.we can declare directly in class itself..

    please let me know answers for the questions..

    • In simple terms, imagine we are going to build a colony of houses and we want all the houses to look similar.

      We will create a blueprint plan and use the same plan for all the individual houses.

      Here blueprint is the interface and individual houses are the concrete classes that will implement the interface.

      • and the interface has oly the blueprint(i mean the methods), the conrete classes which implements that interface can have different definitions r8???
        Correct me if im wrong….

    • Hey, I would like to Post one more example here..
      There are various motor-car producing company.
      Cars produced by each and every company has wheels and breaks.

      All the company is implementing an interface, let’s say “Car” and the vehicle they produce is a concrete class.
      No matter what kind of wheels and breaks they provide, but they need to provide it compulsorily.

      When a class implements an interface, it adheres an agreement that it will provide the implementation of that interface…

  10. Nice explanation sir.But Multiple Inhertance means implementing some multiple properties.Interface also help to implement many property at same class..It cant to say class property or method property.it simply take “property”.

  11. yes i agree ,

    This topic compel me to think about the inheritance. How do you think about the composition in java?

    Which is better way to reuse the things as far as the design and performance is concern inheritance or composition?

    • Prefer composition over inheritance as it is more flexible. Flexible in the sense, in composition we can use DI (dependency injection), but with inheritance the relationship is tightly coupled.

  12. Sir if u get some time then please discuss about.

    Why we have compiler and interpreter in java and other questions like:-

    1. Why we have two system software if both can be equally capable of doing the things like both converts a computer language one level to another.

    2. If both system software is there in lots of languages like java, then it means there is something that can’t be achieved by other one . what is that?

    3. And the last but not the least a programming language is created first or its compiler or simultaneously tell me .

    • hey java is platform independent. it doesn rely on any platforms like linux,solaris.because jvm interprets everything so no need of conversion from one language to other.

      when ever u write a code in java while saving u give as .java but after compilation it becomes .class file.

  13. Good Explanation Joe……..
    Regards
    PMR

  14. I want to know one thing that how multiple inheritance is handled in C++?
    In our example which method will be used from Class B or C???
    And why it is crappy sometimes?

  15. Missing the point.

    1. Interfaces are inheritance of interface, but not the inheritance of implementation.

    2. MI is very useful, because you can mixin implementation.

    3. MI is hard to implement if you have dynamic loading of classes.

    It’s point 3 for the simple reason why it was left out. Think about how dispatch is handled, to see why.

    4. C++ is awful at MI.

    That’s the reason for arguments that you make. C++ is bad at MI, so MI must be bad. That’s the usual argument.

    So what’s a good implementation?

    See Eiffel, and how its dispatch mechanism works, or Python for an interpreted example.

  16. Thats why I like this blog….hard things seem so simple here……it’s like a flow when reading the article….and I can’t stop reading till it ends…..

    Thanks Joe

  17. @Nick,

    Thanks for the comment. I have rephrased some sentences to emphasize the right point.

  18. @Joe

    Constants declared in interfaces are inherited to implemented classes.So there is inheritance through interfaces. Am I right?

  19. In Synchronize static method JVM block the Class then what is its meaning.What is its basic need?Why we do it……..?

  20. I think Gosling took the multiple inheritance out of context. Different programmers have their different views on how to confront a certain issue. What just Gosling exposed is that he has given up his hope and abandoned the language for which he forever loves most- C++.

  21. Thanks for your In-depth clarification of multiple inheritance. Usually I used to concentrate only on definitions but here its something special in your blog.

    Still it would be better to have a separate blog on INTERFACE with its needs, purpose and the right place to use it.

    Waiting for that….

  22. It was a good presentation. Thank You Joe!

  23. good explanation on multiple inheritance.

  24. Superb Awesome……………….. U rock JOE>>>>>>>>>>>>>>>>>>>>

  25. good explanation but we want some additional explanation related to coding’s point of view that may help us for further processing………

  26. Thanks and good website to get deep understanding of Java

  27. Impressive words used here not even bored for a second.I hope i will get here my solution in
    satisfied manner.

  28. Almost every modern language now has a method for multiple inheritance. The diamond problem was never “a problem”…as there is always an order of declaration that can act as the precedence identification. Z.toString, A.toString and B.toString, Z extends A,B, B.toString is the second and final override.

    The idea that multiple inheritance is rarely used is simply untrue. Java has an entire set of metalanguages to deal with this mess (AOP). Opinions being valued over Computer Science started really becoming the norm in the 90′s with these backward decisions on design. Now JVMs would have to be redone to fix this mess.

  29. Thanks Joe for such a nice explanation.

    Interface contain the methods which are implemented by the classes.
    When each class is having its own separate implementation for the methods what is the exact use of having Interface.Cant we directly
    declare and implement the method in the class itself?

  30. Hey joy! nice reasoning, really we can live without multiple inheritance. The thing is that actually multiple inheritance confuses both the programmer and tester and “deadly diamond situation” is a great example of it. Particularly in the case of overridden methods (if java allows multiple inheritance) which class’s method should be overridden is really gives rise to deadlock( mean to say deadly diamond situation). But, however we can survive without multiple inheritance and if necessary we have interfaces to achieve our desired results, so I also agree with you that ” not supporting multiple inheritance is not a flaw in Java programming language”.

    PS: the figure explaining multiple inheritance(heavenly horse inheriting wings from eagle and horse features from ancestors) is really beyond expectations…!

  31. Hi Joe,

    My humble suggestion on your blog is that we can not supremely rule out the issue that Java does not support multiple inheritance. Because, we defer the implementation of the interface methods to the subclass which implements that interface. The interface not only contains methods but some properties too (in the form of public static final ). In this context, the class which implements the interface, it automatically inherit the properties as well as the behaviour from the interface. Just imagine a class extends some abstract class(which contains some properties and abstract methods) and implements some interface (which contains some properties and abstract methods). In this case, the subclass has all properties and behaviours of both the abstract class and the interface. Hence, my point of emphasis is that, we java provides the multiple inheritance support indirectly and it does not nullify the issue, what ever the reason we avoid the multiple inheritance.

    Padmanaban

    • rightly said and we cant say that interface members are not inherited to the class which is implementing this interface.variable are inherited to the subclass.

  32. Really superb presentation.Keep this spirit!!!

  33. Hi,

    Could you please explain about ThreadPoolExecutor in your usual way. Thanks in advance

  34. great explanation! Thanks for this awesome write-up..

  35. Can an interface extend two interfaces?
    Eg: interface A extends B,C{

    }

    • yes it would support multipe interfaces but not multiple inheritance be clear

      • But sir,
        many argue this as multiple inheritance, even I have read it from some books and sites.

  36. Right I am Agree with you sir .
    Another region is inheritance is totally based on object but in interface we can not create an object of interface so we can’t say that .
    thanks sir .

  37. there is no vitual keyword in java to stop duplicacy of function so there is no multile inheritance occur in java in my opinion

  38. what is the alternate solution for Multiple inheritance in JAVA?

  39. interface A{
    public void m1();
    }
    interface B{
    public void m1();
    }

    public class InterfaceTest implements A,B{
    public void m1(){
    System.out.println(“method…”);
    }
    public static void main(String[] args) {
    InterfaceTest it=new InterfaceTest();
    it.m1();
    }
    }

    Could you please explaine while call this method which method will call.

    • A a1=new I1();
      a1.m1();now its call method available in interfaceA

      B b1=new I1();
      b1.m1();
      ;now its call method available in interfaceB

      • A a1=InterfaceTest();
        a1.m1();now its call method available in interfaceA

        B b1=new InterfaceTest();
        b1.m1();
        ;now its call method available in interfaceB

    • interface A
      {
      public void m1();
      }
      interface B
      {
      public void m1();
      }

      public class InterfaceTest implements A,B
      {
      public void m1(){
      System.out.println(“hai”);
      }
      public static void main(String[] args) {
      InterfaceTest it=new InterfaceTest();
      A a=new InterfaceTest();
      a.m1();
      B b=new InterfaceTest();
      b.m1();
      it.m1();
      }
      }

      so this would gives my clarity to u r question it depends
      on which object is being created first and assigning of reference to

      • please explain me clearly what happens when you write B b=new interfacetest();internally how it works…

  40. good…Can u tell me the difference between Access Modifiers and Access specifiers?

  41. nice explanation, my opinion, down arrow instead of up arrow will exactly express the inheritance.

  42. Hi Joe,
    Every class inherits an object class. So when one class acquires the properties of another class it also acquires the properties of the object class. Multiple Inheritance is already happening there. So what’s the point in saying Java does not support Multiple Inheritance.

    • Multiple Inheritance is the concept of a single class inheriting from two or more super classes. If a class inherits from a super class and if that super class inherits from another super class it does NOT qualify as Multiple Inheritance. It is still Single Inheritance.

      It is true that every class in Java inherits from the Object class – either indirectly or directly.

      e.g.
      Class Sparrow inherits from class Bird and class Bird in turn inherits from class Animal, which in turn inherits from Object. This is still Single Inheritance. It will be Multiple Inheritance if the Sparrow class inherits from another class Flier along with inheriting from the Bird class. Hope it helps.

    • in dealing with inheritanc we should concentrate on creation of object to super class to for example

      public class superclass {
      superclass()
      {
      System.out.println(“constuctor create means object created”);
      }
      public void a()
      {
      System.out.println(“a of superclass”);

      }
      }

      the sub class would only extend if and only if it does not extend any other class
      once u create object of sub class the object hierachy would be create on order of objectclaass–>superclass—>subclass;
      public class subclass extends superclass{
      int a=99;
      subclass(int a)
      {
      super();

      }
      superclass s;
      public void a()
      {
      System.out.println(“this is funa of subclass”);
      super.a();
      }
      public static void main(String[] args)
      {
      subclass s=new subclass(2);
      System.out.println(s.a);
      s.a();

      }

      }

    • hey jobin, wht eg u gave is the example of multilevel inheritance…

  43. why we keep ‘final’ of Interface data member…? please give me reason !

  44. hi it’s nice article , i also went through White paper it says
    “Multiple inheritance—and all the problems it generates—was discarded from
    Java. The desirable features of multiple inheritance are provided by
    interfaces—conceptually similar to Objective C protocols.”……. why cant we say that

  45. Awesome article and beautiful comments.
    I teach lot of things from this article and comments..

  46. nice article…and explanation is very good

  47. very interesting… there cant be a better explanation for this… thanx

  48. interface A{
    public void ABC();
    }
    interface B{
    public void ABC();
    }
    public class Example implements A,B{
    public void ABC(){
    // implementation of ABC
    }
    p s v m(String…){
    Example e = new Example();
    e.ABC();//if we implement and call the //method, now this method reference to which //interface, I mean how the jvm invokes the method call ?
    }
    }

  49. Thanku joy vr\ery good concept ….i was eaasily understand….

  50. really a worth reading it…
    nice explanation …

  51. nice article..keep posting this kind of article in java..thanks so much

  52. Awesome explanation.
    Nice articles. Please publish a complete book with all of your articles.

  53. Hi Joseph,

    Instead of avoiding the multiple inheritence altogether, the java specification could have provided a solution for this multiple inheritence implementation issue. One solution, I could think is, the compiler can add a restriction to redefine the method if it is being defined in more than one parent (Base) classes. Inside the redefinition, the subclass can call one of the base class definition.

    thanks.

  54. What ever it is so it is. I appreciate your effort to make programmers clear their concepts. I just want to add one thing, and please correct me if I am wrong. We all know ‘Multiple’ means more than one ‘Inheritance’ means adopting attributes and characteristics from parent. In java if A extends B it means A acquires all(I dont want to discuss what can not be) the characteristics of Class B. So B is one parent of A (single inheritance). But by default another parent of A is Object class. So from A’s perspective it has two parents B and Object. Is it not a contradiction with the statement that Java does not support multiple inheritance.

    • object class would extended in conditions like if it does nt extend any class the defalutly it would not be extended

  55. Nice article and good reasoning.
    thank you sir.

  56. I am new to the java programming but I understud all above about multiple inheritence. it is very interesting.thnx a lot.

  57. finally i understand this concept,,thanks a lot,smart explanation

  58. Hi!
    what is diff b/w abstract and interface based on the usage and not based on syntax ?

    why we use interface,because already over abstract?(the same functionality done by abstract)

  59. thank u so much i was wandering for this type of explanation :)

  60. Very Well explained. Thanks a lot. Keep posting

  61. I agree with most of your point. What ever the reason are but i consider it as weakness of Java. As it works well in other some language

  62. Hi joe,
    the implementing class will have only one method and that too will be done by the implementer.

    which class it will be can you provide me some more explanation for these lines

  63. sir give me guidance plz i am pursuing MCA last semester and i have decide e-shopping project on J2EE so give me guidance how to online payment facility implement or your suggestion give me sir……Lalit

  64. excellent and nicely explained thank you

  65. Java Supports Multiple Inheritance.
    But, But in restricted sense – Single Concrete super-type and multiple “interface only” super-type.

    Where as in language like C++, multiple concrete super-type.

    This restriction, enables java to get away with most problems of multiple inheritance.

  66. By default every class in java inherits Object class, then how can one say that java does not support multiple inheritance?

    class A{
    //implicitly inherits Object Class
    }

    class B extends A{
    //implicitly inherits A, Object class too??
    }

    Please Help.

  67. Can you post a blog on ‘context’ in java? What exactly it is? We use it in servlet,android ,ejb

  68. Hi joe,
    Today actually i came to know about the concept behind why multiple inheritance is not supported by java…u r superb

    Thanks,
    Tapu Ranjan

  69. I always Google, when i am in doubt and you papers are the first thing that shows up

    Thanks,

  70. Pingback: Observer Design Pattern

  71. Really nice post sir,,,
    i like your site i always use for reference.
    thanks to you to increase my knowledge…

  72. simply superb…….. i like your site very much.

  73. i haven’t understood this means. u made
    me confused alot.please make understood .

    How multipale inheritance is not possible in
    java ?

  74. This is really an amazing post and came to know that why java does not support Multiple inheritance…. Keep Posting you are doing a great work……..

  75. Very clear explanation. we can’t get the method implementation of those interfaces. So it proves java does not support multiple inheritance. Thanks Joe

  76. Many thanks, I really appreciate it, a well-chosen article and content.

    Regards
    Waleed, UAE

  77. Hi Joe,

    As we all know java does not support multiple inheritance,but provides a useful way of Interface.
    All the explanation which i got is related to methods not about the data member in this regard.

    Following example denotes this.

    public interface TempIntfA {
    public static int MAX=10;

    }

    public interface TempIntfB {
    public static int MAX=20;
    }
    public class ClsImplIntf implements TempIntfA,TempIntfB{

    public static void main(String a[])
    {
    ClsImplIntf c=new ClsImplIntf ();
    int c2=c.MAX;
    }
    }

    the line gives the compilation error that “The field c.MAX is ambiguous.”

    with this can i say that “java support multiple inheritance through interface in case of methods but not in case of data members”

    Thanks,
    Abhishek

  78. Jaywant Patil Java Developer said on ,

    Very clear explanation.
    This is really an amazing explaination and came to know that why java does not support Multiple inheritance…. Keep Posting you are doing a good work…….

    Thanks & Regards
    Jaywant Patil

  79. Awesome explanation dude…,
    Got what I needed. Thanks bro.

  80. Let me know when to use interface and when to use abstract class

  81. easy defination for multiple inheritance

  82. Hi,

    Your example for the justification looks good.
    But we can create a obj for D class, and any one or both methods can be implemented.
    that can be accessed by differentiating parameters.

    Is it so?

  83. Totally thanks to you for this article…
    There are many reasoning in the world of internet but this was the ideal way to understand this reasoning that why multiple inheritance is not included in java..

    Just as the definition of java…
    SIMPLE…!!!!

  84. An awesome example of bird and horse.
    Will like to see more on java.
    Thanks one again for clearing my concept of the Multiple inheritance.

  85. Hi joe,

    I want to know something about inner and nested classes please give me a best explanation of those ,

    Thanks in advance

  86. it was very satisfiable .. thnkew for removing my cnfusion..

  87. I want to ask one question to Mr. Joe sir ,
    that why we can not create the object of Abstract class ?

  88. after handling exception can we again start the execution of programme ?

  89. Hi Joe,

    Even I agree with your view that omission of Multiple inheritance is for our good only in order to avoid confusion. You have explained it well with an apt analogy..

  90. one reason for java does not support multiple inheritance is that because of multiple inheritance root object is created multiple times which is wrong and also some variable and method present in the same object with same priorities which leads jvm enter into the state of ambiguity.

  91. Nice Article. The valuable content is presented in the simple way. Kudos!

  92. good explantion of multiple inheritance .how we can expain with teory bases .how we can debate that there is not supported in java explain.

  93. Hope.Nobody will not struck with this question for upcoming interviews, who has open this blog.

  94. Multiple Inheritence is possible in java by Interfaces

  95. Hi joe

    It’s pleasure to read such explanation. It’s
    Really very good article.

  96. nice article.

    Thanks for the clear explanation by taking the James Gosling’s words.

  97. This is very nice explanation dear,
    it really interesting and meaningful one..

    Thanks for sharing..

  98. Thanks a lot! :)

    But is it just because of simplicity that it was committed or it is because of some features which java may not have supported? In C++ we use “::” for resolving ambiguity.. I know it may sound strange, but why didn’t they give this feature?

    Avishek

  99. Hi Joe,
    Best clarification of, Why java does not support or not at all need such kinds of ambiguous implementations…So No confusion..i.e. its a real beauty and simplicity of java..

  100. class b{

    b()
    {

    }

    }
    class c
    {
    c()
    {
    }
    }
    //if supports multiple inheritance
    class d extends b,c
    {
    d()
    {
    super();
    /** in the above statement is confusion occurred which class constructor will call either class b or class c so thats why java doesnt support**/
    }
    }

    • Rangareddy,

      I think if you revisit constructor calling hierarchy that would answer your question.

      When we instantiate a class it first call constructor of its super class before executing its own constructor.

      In your example, JVM would not be able to identify the super class constructor which needs to be called when you create object of class D.

  101. Interface A {
    int i=10;
    }
    Interface B {
    int j=20;
    }

    Class C implements A, B {

    public static void main(String s[]) {
    System.out.println(“i=”+i);
    System.out.println(“j=”+j);
    }
    }

    My doubt is, can we say that there is no multiple inheritance(even a partial MI) in this situation.

  102. Joe Nice explanation. But imho for getting more polymorphism i.e the flexibility of both the compositon and flexibility of polymorphism in one design is a good design principle. I think in those days to make java simple than C++ which of course complex which justify the name of the Java definition they have made interface inheritance. But for implementation inheritance now I think there should be some workaround.

  103. i have a doubt!!!!
    a java class extends Object as default….r8…then if we extends another class its multiple inheritance r8….

    is it r8 or wrong please give me an answer

  104. hI, in multiple inheritance compiler wont compare any methods of classes. it will thrown error because of constructor . why because compiler will create default constructor to class automatically so, it will confuse initially to which class it has to create constructor. there we will get ambiguity ….

    :-)

  105. hI, in multiple inheritance compiler wont compare any methods of classes. it will thrown error because of constructor . why because compiler will create default constructor to class automatically so, it will confuse initially to which class it has to create constructor. there we will get ambiguity ….

    :-)

  106. May be it is time to say till Java 7. With JDK 8, default functions coming into interfaces, what do you think?

  107. Really helpfull blog for the people who are little bit confusing about the why java dosen’t support multiple inheritance.

  108. java not support multiple inheritance…but i don’t understand the java.lang.object. class which is the root class of every class.

    Now, my Question is: Class A base class of Object class and also the Class B….? this is the multiple inheritance

    • That example is not comes under multiple inheritance it comes under Hierachical inheritance.

  109. Most languages are adding support for multiple inheritance, including Java in Java 8. Conceptually, single inheritance is simpler at the cost of making the overall program harder and more verbose to express.

    The diamond problem is simple to resolve. See Eiffel, Scala and Haskell for how useful multiple inheritance is.

    I think your claim that multiple inheritance is rarely used is a poor one. In java MI is banned, so developers don’t realise when they actually need MI. They work around it by reimplementing MI using composition at the cost of extra code and defects.

    I think you could learn alot by learning more fundamentals of computer science, rather than being Java focussed.

  110. simple and clear.. thanx for such a nice explanation

  111. nice explanation..really got ur message

  112. “Dynamic loading of classes makes the implementation of multiple inheritance difficult”
    Please expalin me this..
    Thanks

  113. its very nice explanation and its very usefull to me and example picture are good easy to understand also thanks

  114. The argument is quite weak actually. “…rarely used, poorly understood, confusing…” – is an example of a qualifying statement without any proof or basis – just an opinion that is “supposed” to be taken as gospel.
    Is it an excuse to stay ignorant? Is it akin to “don’t use the hammer because you will hit your head with it”? Rediculous.
    The diamond “problem” is so easy to avoid – it’s not even worth a discussion. The key is that it is resolved at _compile_ time, where it is most appropriate to give an error if it’s ambiguous. Done. Before saying “Nooo..” try adding the same-named constant to two idependent interfaces, implement them both by the same class and reference the constant. Got it? There you go. Why not do the same for multiple inheritance? Just in case you still don’;t get it:

    interface A {
    public static final int i = 0;
    }

    interface B {
    public static final int i = 1;
    }

    class C implements A,B {
    int a() {
    return i;
    }
    }

    ————–
    C.java:xx: reference to i is ambiguous, both variable i in A and variable i in B match
    return i;
    ^
    1 error
    ==============

    What about implementing everything just once? Try that with this:

    abstract class A {
    private int a;

    public int getA() { return a; }
    }

    abstract class B {
    private int b;

    public int getB() { return b; }
    }

    // needs both “a” and “b”
    class C extends A,B {
    // ??? ether this or you have to RE-IMPLEMENT
    }

    Copy/paste anyone?

  115. Joe,

    It is nice article on Multiple Inheritance. you have images to explain and it is very easy to understand. But then what is the use of Interface in java?. i suppose, we use interface to achieve multiple inheritance. Can you clearly explain about that?

    Thanks,

  116. great explanation…so simple like JAVA …:)

  117. THis IS a Great Aricle AND Great discussions are discussing over this website
    ThAnks for the information

  118. first of all i like to say thank you to clarified my doubts in java

  119. we have implemented multiple inheritance using interface

    class first2
    {

    int a=10;
    }
    interface first3
    {

    int b=12;
    }

    class first1 extends first2 implements first3
    {
    int j=3;
    }

    class first
    {
    public static void main(String args[])
    {
    first1 ob = new first1();
    System.out.println(“j :”+ ob.j + ” a: ” +ob.a + ” b : “+ob.b);

    }
    }

  120. nice explanation. very easy to understand.
    Thanks sir

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>