Home arrow static arrow Java Programming [Archive] - extend several abstract classes
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - extend several abstract classes
This topic has 8 replies on 1 page.

Posts:56
Registered: 9/12/99
extend several abstract classes  
Aug 6, 2004 7:47 AM



 
Folks:

Is it possible to extend several abstract classes in Java? If so, how does it look like?

I tried the following

public class extends AbstractClass1, AbstractClass2
{
}

Please help, thanks a lot !
 

Posts:24,036
Registered: 2/3/03
Re: extend several abstract classes  
Aug 6, 2004 7:48 AM (reply 1 of 8)



 
Folks:

Is it possible to extend several abstract classes in
Java?

Nope. But you can implement multiple interfaces.
 

Posts:24,036
Registered: 2/3/03
Re: extend several abstract classes  
Aug 6, 2004 7:48 AM (reply 2 of 8)



 
[url=http://mindprod.com/jgloss/interfacevsabstract.html]Java Glossary : interface vs abstract class[/url]
[url=http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html]Abstract classes vs. interfaces: When does it make sense to choose an abstract class over an interface?[/url]
[url=http://www.javaworld.com/javaworld/javaqa/2001-08/03-qa-0831-interface.html]Abstract classes and interfaces practicum: Move from theory to practice on when to employ abstract classes vs. interfaces[/url]
[url=http://java.sun.com/developer/JDCTechTips/2001/tt1106.html#tip2]Tech Tips: ABSTRACT CLASSES VS. INTERFACES[/url]
 

Posts:37,103
Registered: 3/30/99
Re: extend several abstract classes  
Aug 6, 2004 7:50 AM (reply 3 of 8)



 
What you're asking about is multiple inheritance of implementation. It's present in C++ (and other languages, I assume) but was left out of Java on purpose, presumably because the times when it's truly valuable are rare relative to the mess it causes when it's misused, which is often.

What you get with Java's interfaces is multiple inheritance of interface. This is more widely useful and less dangerous than MI of implementation. (IMAO)
 

Posts:56
Registered: 9/12/99
Re: extend several abstract classes  
Aug 6, 2004 7:54 AM (reply 4 of 8)



 
But my problem is that I don't want to define the methods declared in an interface. Rather, I want to inherit ( use them directly) methods in multiple classes. I am sure I can just put all the methods I want to inherit into one single parent class. But I don't think it's nice to do that.

Any clue?
 

Posts:24,036
Registered: 2/3/03
Re: extend several abstract classes  
Aug 6, 2004 7:54 AM (reply 5 of 8)



 
Here's a pretty good explanation of the problems with multiple inheritance:

multiple inheritance

[url=http://www.javaworld.com/javaworld/jw-12-1998/jw-12-techniques.html]Interfaces and the 'diamond problem'[/url]

One justification of interfaces that I had heard early on was that they solved the "diamond problem" of traditional multiple inheritance. The diamond problem is an ambiguity that can occur when a class multiply inherits from two classes that both descend from a common superclass. For example, in Michael Crichton's novel Jurassic Park, scientists combine dinosaur DNA with DNA from modern frogs to get an animal that resembled a dinosaur but in some ways acted like a frog. At the end of the novel, the heros of the story stumble on dinosaur eggs. The dinosaurs, which were all created female to prevent fraternization in the wild, were reproducing. Chrichton attributed this miracle of love to the snippets of frog DNA the scientists had used to fill in missing pieces of the dinosaur DNA. In frog populations dominated by one sex, Chrichton says, some frogs of the dominant sex may spontaneously change their sex. (Although this seems like a good thing for the survival of the frog species, it must be terribly confusing for the individual frogs involved.) The dinosaurs in Jurassic Park had inadvertently inherited this spontaneous sex-change behavior from their frog ancestry, with tragic consequences.

This Jurassic Park scenario potentially could be represented by the following inheritance hierarchy:

          Animal
/ \
Frog Dinosaur
\ /
Frogasaur


The diamond problem can arise in inheritance hierarchies like the one shown in Figure 1. In fact, the diamond problem gets its name from the diamond shape of such an inheritance hierarchy. One way the diamond problem can arise in the Jurassic Park hierarchy is if both Dinosaur and Frog, but not Frogosaur, override a method declared in Animal. Here's what the code might look like if Java supported traditional multiple inheritance:

abstract class Animal {     abstract void talk();} class Frog extends Animal {     void talk() {         System.out.println("Ribit, ribit.");} class Dinosaur extends Animal {     void talk() {        System.out.println("Oh I'm a dinosaur and I'm OK...");    }} // (This won't compile, of course, because Java// only supports single inheritance.)class Frogosaur extends Frog, Dinosaur {}

The diamond problem rears its ugly head when someone tries to invoke talk() on a Frogosaur object from an Animal reference, as in:

Animal animal = new Frogosaur();animal.talk();


Because of the ambiguity caused by the diamond problem, it isn't clear whether the runtime system should invoke Frog's or Dinosaur's implementation of talk(). Will a Frogosaur croak "Ribbit, Ribbit." or sing "Oh, I'm a dinosaur and I'm okay..."?

The diamond problem would also arise if Animal had declared a public instance variable, which Frogosaur would then have inherited from both Dinosaur and Frog. When referring to this variable in a Frogosaur object, which copy of the variable -- Frog's or Dinosaur's -- would be selected? Or, perhaps, would there be only one copy of the variable in a Frogosaur object?

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.
 

Posts:24,036
Registered: 2/3/03
Re: extend several abstract classes  
Aug 6, 2004 7:55 AM (reply 6 of 8)



 
But my problem is that I don't want to define the
methods declared in an interface. Rather, I want to
inherit ( use them directly) methods in multiple
classes. I am sure I can just put all the methods I
want to inherit into one single parent class. But I
don't think it's nice to do that.

Any clue?

Yes, think composition rather than inheritance.

[url=http://www.artima.com/designtechniques/compoinh.html]Composition versus Inheritance[/url]

[url=http://java.sun.com/docs/books/tutorial/java/concepts/]Object-Oriented Programming Concepts[/url]
[url=http://www.javaworld.com/javaworld/jw-04-2001/jw-0406-java101.html]Object-oriented language basics[/url]
[url=http://sepwww.stanford.edu/sep/josman/oop/oop1.htm]Don't Fear the OOP[/url]
 

Posts:56
Registered: 9/12/99
Re: extend several abstract classes  
Aug 6, 2004 7:58 AM (reply 7 of 8)



 
Yes, dude. That is exactly what I want and exactly what I need to do.

Thanksssssssssss !
 

Posts:37,103
Registered: 3/30/99
Re: extend several abstract classes  
Aug 6, 2004 8:01 AM (reply 8 of 8)



 
But my problem is that I don't want to define the
methods declared in an interface. Rather, I want to
inherit ( use them directly) methods in multiple
classes. I am sure I can just put all the methods I
want to inherit into one single parent class. But I
don't think it's nice to do that.

Any clue?

Inheriting just to share code is the wrong reason to inherit. You can share code via composition/delgation too. True, you have to actually type the method signature and the single line to call the delgate, but that's trivial.

For example...
interface I1 {   void foo();} interface I2 {    void bar();} interface I1I2 extends I1, I2 {} class I1Impl implements I1 {    public void foo() {        // bunch of code you want to share    }} class I2Impl implements I2 {    public void bar() {        // bunch of code you want to share    }} class I1I2Impl implements I1I2 { // or implements I1, I2    I1 i1_ = new I1Impl();    I2 i2_ = new I2Impl();     public void foo() {        i1_.foo();    }     public void bar() {        i2_.bar();    }} 
 
This topic has 8 replies on 1 page.