aspose file tools
The moose likes Beginning Java and the fly likes Multiple inheritance in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multiple inheritance in java" Watch "Multiple inheritance in java" New topic

Multiple inheritance in java

sreedhara satuluri

Joined: Mar 17, 2005
Posts: 12
Interface is the key feature with which we can implement multiple inheritance in java.
But it is seen in many sites that java does not support Multiple inheritance.
please give me some examples
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

Java supports multiple inheritance of types. A class can have many types, each defined by an interface. But Java does not support multiple inheritance of implementation. A class can inherit actual code from just one other class.

Not every language makes this kind of distinction.

[Jess in Action][AskingGoodQuestions]
sreedhara satuluri

Joined: Mar 17, 2005
Posts: 12
there was a heated discussion in that affair and it went very long but no solution
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
This is the heart of polymorphism: A class that implements multiple interfaces can be treated as though each its objects were an object of that interface's type, so the object could be added to a collection of the interface type.

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Implementing multiple interfaces is a very limited version of multiple inheritence because it does not allow you to inherit any implementation, as stated above. In contrast, C++ allows full-blown multiple inheritence. That is it allows one class to "extend" multiple base classes. Each of the base classes are full classes, which means they can include some implentation details if necessary.

When people say that Java does not have multiple inheritence, they typically mean that a class cannot inherit from multiple base classes. Many other object-oriented languages provide full support for multiple inheritence whereas Java's support is very limited.


Java API Documentation
The Java Tutorial
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by sreedhara satuluri:
there was a heated discussion in that affair and it went very long but no solution

Depends on what you call a solution... Of course the people screaming that Java should have multiple inheritance a-la C++ say there's no solution, but sane people agree that there is a perfectly working solution and that's interfaces.

Avisesh Jain

Joined: Mar 16, 2005
Posts: 12
One justification of not allowing multiple inheritance in JAVA is to avoid the well known diamond problem with multiple inheritance in C++. Let me give a quick explanation about the diamond problem. Consider a class called Parent which has a method called MyMethod, now let two classes say Child1 and Child2 inherit from this class Parent, each of them have an implementation of the method MyMethod. Now Consider a class GrandChild which multiply inherits from Child1 and Child2 and it does not have an implementation of MyMethod. Now if an Object of GrandChild makes a call to MyMethod there is an ambiguity as to which method should be called.

In Java, interfaces solve all these ambiguities caused by the diamond problem. Through interfaces, Java allows multiple inheritance of interface but not of implementation. Implementation, which includes instance variables and method implementations, is always singly inherited. As a result, confusion will never arise in Java over which inherited instance variable or method implementation to use.
M Beck
Ranch Hand

Joined: Jan 14, 2005
Posts: 323
the Python programming language also has multiple inheritance (of implementation), and frankly, i think it's barely worth its effort from what i can see. it brings with it some fairly hairy issues of precedence resolution that's taken several language revisions and several years to get more or less resolved. (see, for example, version 2.2.1, version 2.3.1, or -- for purely archaeological interest -- version 1.5.2.)

multiple inheritance (of either major kind) enables the use of Mix-ins, which is a major win. interface inheritance forces you to rewrite the code for the mixed-in methods in each class you use them in, which might create a good deal of duplication; but multiple implementation inheritance gives you the "diamond rule" headache. any problem that needs a bunch of Common Lisp/Dylan aficionados to write a scholarly paper about its solution (this paper) is not a problem you want to tackle lightly.
It is sorta covered in the JavaRanch Style Guide.
subject: Multiple inheritance in java