Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Multiple Inheritance
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Okapi




PostPosted: Wed Nov 12, 2008 8:31 pm   Post subject: Multiple Inheritance

Is there anyway to use multiple inheritance with abstract classes?

Simple example:
abstract class Pet(){

//abstract methods
}

abstract class Dog extends Pet{
}

abstract class Cat extends Pet{
}

Is there anyway a class CatDog (freak of nature) extend both Cat and Dog?
Sponsor
Sponsor
Sponsor
sponsor
Vermette




PostPosted: Wed Nov 12, 2008 9:53 pm   Post subject: Re: Multiple Inheritance

Java by design does not support multiple inheritance. There is a lot of argument as to whether this is a good or bad thing (most say bad), but sticking to a strict hierarchy design was done to avoid some types of design problems that can creep in.

To capture common behaviour between classes you can define an Interface.

edit: http://en.wikipedia.org/wiki/Interface_(Java)
pavol




PostPosted: Wed Nov 12, 2008 9:57 pm   Post subject: RE:Multiple Inheritance

I'm not sure if there is any way to do that, but the way I would solve the problem is to try and put the Dog and Cat classes together into one so you don't have to extend both. I guess what I'm saying is if you are going to use Dog and Cat to just try to make CatDog, then make CatDog only and you won't have to extend anything except for Pet.
Is this the problem you were having? with the Dog and Cat classes or was it just an example and you were curious if you can extend two classes?
Vermette




PostPosted: Wed Nov 12, 2008 10:07 pm   Post subject: RE:Multiple Inheritance

Here, this looks to be a decent article that talks about both abstract classes and interfaces in Java and how to use them:

http://www.javabeginner.com/abstract-class-interface.htm
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: