I am aware that java does not support multiple inheritance. And I have heard people saying , multiple inheritance can be achieved using the concept of interface. But I not able to understand how they are achieved using interface ? So does java support multiple inheritance or not? When there is a need for an interface to extend more than 2 interfaces ? And why do we need interface first of all ? -raja
Somewhere someone is dreaming of your smile
Joined: Jan 04, 2003
hi raja veeramani
multiple inheritance can be achieved using the concept of interface.
But I not able to understand how they are achieved using interface ?
It is a concept of polymorphism that the class implements several interfaces, for example the original class named service provider who contains three services, these three services are implement by service provider, so that the service provider can act as three role such as cook, cleaner and feeder. Each role resopnds to be a specific function(abstraction). So that the service provider can use the function of interfaces and to be a role of these interfaces, you can act as which role at run time that is up to you. And also appying some OO design principles such as low couping and high cohesion.
So does java support multiple inheritance or not
No java does not support multiple inheritance, that means you can not extends more than one class in the original class hope this help
[ April 12, 2004: Message edited by: siu chung man ]
Joined: Feb 08, 2004
This depends on your definition of "multiple inheritance". When most people say you can achieve multiple inheritance using Java interfaces, they are not using a very rigorous definition of MI. What most people mean is: if a single object implements many interfaces, then you can treat that object polymorphically as any of those interfaces. Example:
So, because Foo implements both Bar and Baz, it must implement the APIs of both of those interfaces. Because it expresses this relationship explicitly, the compiler allows me to treat objects of class Foo as any of the following types: Foo, Bar, or Baz. Example:
At this point, I have three references to the same object. That object is of *class* Foo. In Java, class is immutable--once an object is created, it is of a certain class and it will always remain of that class until it is garbage collected. As proof of this, you can write:
That code will print "Foo" three times. The point is, regardless of the *type* that is being conferred upon an object by the reference used to access it, the *class* of an object does not change. What does change, however, is its type. If I use b1 to access that Foo object, I am referring to it polymorphically as a Bar. If I use f, then I am treating it as type Foo (which also happens to be its class). Because that one object can have all three types conferred upon it by different references, most people will say this is how Java achieves "multiple inheritance". So you can "multiply inherit" from interfaces. But can you do it for classes? No, not in Java. In fact, I would argue that implementing multiple interfaces is something more like "multiple implementance", not really multiple inheritance. Strictly speaking, to fully achieve MI, in my mind at least, we're talking about extending classes, not implementing interfaces. MI is about inheriting behavior (method bodies, implementations on abstract or concrete classes, not only abstract methods on interfaces). More to the point, I've been able to come up with a checklist of four items that I think defines inheritance. A class Sub can be said to "extend" Base if all of the following are true:
objects of type Sub can be polymorphically treated as objects of type Base.
changes in method bodies in Base are *automatically* inherited by Sub, without having to update code in Sub.
if ref instanceof Sub == true, then ref instanceof Base == true as well.
Sub can override methods in Base
If a class Sub can be said to have this relationship with multiple classes Base1, Base2, ..., BaseN, then you have MI. I have figured out a way to manually allow a class Sub to extend more than one base class using a combination of interfaces and inheritance, but I've only found occasion to use it when I absolutely had to have MI (which is to say, never--it was a purely academic exercise from the beginning and has remained such since I thought it up). Still, it's interesting though, and I'd be happy to post it here if you're interested. sev