File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple inheritance in java

 
sreedhara satuluri
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sreedhara satuluri
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there was a heated discussion in that affair and it went very long but no solution
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Layne
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 323
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic