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.
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 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.
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.
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.
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.
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 are closed for "Why Multiple Inheritance is Not Supported in Java".
simply superb
nice 1 keep posting article like this
Nice Job Kulandai… !!!!! I am you fan!!!!
Nice Joe.
Can you please write blog on volatile in java with example.
nice explanation
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 .
Huh, I’m confused. Everything I read said inheritance was a good thing.
worth reading…one can easily get the concept because of diagrams used in it… thanks
very nice..
crystal clear
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..
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”.
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?
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 .
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.
@Nick,
Thanks for the comment. I have rephrased some sentences to emphasize the right point.
@Joe
Constants declared in interfaces are inherited to implemented classes.So there is inheritance through interfaces. Am I right?
In Synchronize static method JVM block the Class then what is its meaning.What is its basic need?Why we do it……..?
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++.
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….
Nice Explanation
It was a good presentation. Thank You Joe!
good explanation on multiple inheritance.
Superb Awesome……………….. U rock JOE>>>>>>>>>>>>>>>>>>>>
Nice Explanation
good explanation but we want some additional explanation related to coding’s point of view that may help us for further processing………
Thanks and good website to get deep understanding of Java
Why Scala can do this?
Impressive words used here not even bored for a second.I hope i will get here my solution in
satisfied manner.
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.
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?
Very good explination..
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…!
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
Can an interface extend two interfaces?
Eg: interface A extends B,C{
}
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 .
there is no vitual keyword in java to stop duplicacy of function so there is no multile inheritance occur in java in my opinion
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.
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.
why we keep ‘final’ of Interface data member…? please give me reason !
Thanks Joe! This is a really nice post.
Very Nice Explanation !!!
nice article,
it has cleared much more about Inheritance.
thanks
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
Awesome article and beautiful comments.
I teach lot of things from this article and comments..
nice article…and explanation is very good
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 ?
}
}
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.
this is awsomeeeee
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.
good…joe
nice article..keep posting this kind of article in java..thanks so much
Awesome explanation.
Nice articles. Please publish a complete book with all of your articles.
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.
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.
Nice article and good reasoning.
thank you sir.
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)
yes it would support multipe interfaces but not multiple inheritance be clear
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
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();
}
}
object class would extended in conditions like if it does nt extend any class the defalutly it would not be extended
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
nice………
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
blog and it content super…….
hey jobin, wht eg u gave is the example of multilevel inheritance…
Very nice explanation. Thanks.
Its a good article.. Nadig
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
excellent and nicely explained thank you
nice..
very nice answer..
Explained in a simple manner. Very nice
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.
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.
Yes Jim, inheritance is always good, in particular for the inheritor :-)
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.
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.
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.
Really nice post sir,,,
i like your site i always use for reference.
thanks to you to increase my knowledge…
simply superb…….. i like your site very much.
i haven’t understood this means. u made
me confused alot.please make understood .
How multipale inheritance is not possible in
java ?
how we can explain it theoritically?
Nice explanation..
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……..
Very clear explanation. we can’t get the method implementation of those interfaces. So it proves java does not support multiple inheritance. Thanks Joe
nice..:)
Good explanation…
Many thanks, I really appreciate it, a well-chosen article and content.
Regards
Waleed, UAE
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
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. )
relly nice exp
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
Awesome explanation dude…,
Got what I needed. Thanks bro.
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?
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…!!!!
after handling exception can we again start the execution of programme ?
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.
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…
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.
its nice explanation
thanks
Nice Article. The valuable content is presented in the simple way. Kudos!
it’iiiiii It’s a superb article
good article. nice way to explain.
good explantion of multiple inheritance .how we can expain with teory bases .how we can debate that there is not supported in java explain.
Nice article
Nice one
nice 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
Welcome Sathish.
Thanks Shekhar.
Nice explanation…
Thanks a lot,
Really worth reading it.
Welcome Naman.
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
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.
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.
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.
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 ….
yes boss exactly correct
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….
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
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.
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?
That example is not comes under multiple inheritance it comes under Hierachical inheritance.
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.
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,