Why Multiple Inheritance is Not Supported in Java

Last modified on June 19th, 2017 by Joe.

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.

Multiple

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.

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.

Comments on "Why Multiple Inheritance is Not Supported in Java"

  1. Thiyagarajan says:

    simply superb

  2. Abhishek says:

    nice 1 keep posting article like this

  3. Anonymous says:

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

  4. Manishkumar Modi says:

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

  5. sunil says:

    nice explanation

  6. Asraful Forhad Chowdhury says:

    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. Jim says:

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

  8. amy says:

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

  9. vadhiraju says:

    very nice..

  10. swarna says:

    crystal clear

  11. Leo says:

    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..

  12. MOHANRAJ says:

    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”.

  13. Arvind says:

    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?

  14. Arvind says:

    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 .

  15. Nick says:

    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. Joe says:

    @Nick,

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

  17. Radhika says:

    @Joe

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

  18. rakesh kumar says:

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

  19. Anonymous says:

    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++.

  20. Praveen J says:

    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….

  21. SrinivasaraoVanapalli says:

    Nice Explanation

  22. Vijay says:

    It was a good presentation. Thank You Joe!

  23. suresh says:

    good explanation on multiple inheritance.

  24. Sarath Kumar N says:

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

  25. Balaji Reddy says:

    Nice Explanation

  26. Amit Singh says:

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

  27. Anonymous says:

    Thanks and good website to get deep understanding of Java

  28. Anonymous says:

    Why Scala can do this?

  29. Sumit Singh says:

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

  30. Jack9 says:

    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.

  31. Akhil says:

    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?

  32. Shubha says:

    Very good explination..

  33. Sultan says:

    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…!

  34. Anonymous says:

    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

  35. Shiv says:

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

    }

  36. basant joshi (B,J) says:

    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. nizam says:

    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. krishnamurthy says:

    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.

  39. Jobin says:

    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.

  40. rakesh kumar says:

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

  41. Harsh says:

    Thanks Joe! This is a really nice post.

  42. Arun says:

    Very Nice Explanation !!!

  43. Rahul sharma says:

    nice article,

    it has cleared much more about Inheritance.

    thanks

  44. deepak says:

    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. Kiran says:

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

  46. mansa says:

    nice article…and explanation is very good

  47. K Shiv says:

    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 ?
    }
    }

  48. usha says:

    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.

  49. siddu says:

    this is awsomeeeee

  50. Om says:

    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.

  51. kalai says:

    good…joe

  52. senthil says:

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

  53. Gayathri Chitnhala says:

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

  54. Muthusamy says:

    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.

  55. Manzar Baig says:

    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.

  56. anand says:

    Nice article and good reasoning.
    thank you sir.

  57. albi jegan says:

    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)

  58. ramakrishna says:

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

  59. ramakrishna says:

    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

  60. ramakrishna says:

    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();

    }

    }

  61. ramakrishna says:

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

  62. Anwar says:

    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

  63. Nagaraju Gurram says:

    nice………

  64. mukesh says:

    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

  65. rana says:

    blog and it content super…….

  66. merul shah says:

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

  67. Nupur says:

    Very nice explanation. Thanks.

  68. Anonymous says:

    Its a good article.. Nadig

  69. lalit says:

    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

  70. gangasani mahesh reddy says:

    excellent and nicely explained thank you

  71. srinvas says:

    nice..

  72. Anonymous says:

    very nice answer..

  73. Sandy says:

    Explained in a simple manner. Very nice

  74. PMJ says:

    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.

  75. rggreg says:

    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

  76. Joe says:

    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.

  77. Joe says:

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

  78. Joe says:

    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.

  79. Joe says:

    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.

  80. this.Ankur says:

    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.

  81. Prakash Gavade says:

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

  82. Rajashekhar says:

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

  83. ramesh says:

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

    How multipale inheritance is not possible in
    java ?

  84. sangram says:

    how we can explain it theoritically?

  85. Rohit Kumar Prusty says:

    Nice explanation..

  86. Rajesh says:

    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……..

  87. Dinesh says:

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

  88. LJ says:

    nice..:)

  89. sivaurbozz says:

    Good explanation…

  90. Waleed says:

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

    Regards
    Waleed, UAE

  91. abhishek says:

    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

  92. Serg says:

    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. )

  93. Rakesh says:

    relly nice exp

  94. Jaywant Patil Java Developer says:

    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

  95. Anonymous says:

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

  96. Arun says:

    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?

  97. Saifuddin Syed says:

    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…!!!!

  98. Balu Nehe says:

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

  99. anil says:

    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.

  100. Pranav says:

    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…

  101. sam says:

    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.

  102. Anonymous says:

    its nice explanation

    thanks

  103. sadesh says:

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

  104. vinoth says:

    it’iiiiii It’s a superb article

  105. Dhiren says:

    good article. nice way to explain.

  106. samir says:

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

  107. Iranna Yalavatti says:

    Nice article

  108. Shiva says:

    Nice one

  109. Anonymous says:

    nice explanation

  110. hemanth says:

    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

  111. Joe says:

    Welcome Sathish.

  112. Joe says:

    Thanks Shekhar.

  113. Anonymous says:

    Nice explanation…

  114. Naman says:

    Thanks a lot,

    Really worth reading it.

  115. Joe says:

    Welcome Naman.

  116. Avishek says:

    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

  117. Shinod says:

    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.

  118. srinixx13 says:

    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.

  119. Ashesh says:

    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.

  120. Sreekanth says:

    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 ….

  121. Sree says:

    yes boss exactly correct

  122. sarwan says:

    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….

  123. yashodhan says:

    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

  124. Mark says:

    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.

  125. comer says:

    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?

  126. manjunath says:

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

  127. pradeep kumar says:

    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.

  128. Baskar says:

    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,

Comments are closed for "Why Multiple Inheritance is Not Supported in Java".