extends multiple classes?

I

Ian Stanley

Hi,
Appology if silly question but can you have a class which extends more than
one other class?
If so what is the correct syntax?
eg class1 needs to extend class2 & class3
thanks
Ian.
 
A

Andrew Thompson

Ian Stanley said:
Hi,
Appology if silly question but can you have a class which extends more than
one other class?

No.

Java can implement any number of interfaces,
but can extend only _one_ class.
 
H

Harald Hein

Ian Stanley said:
Appology if silly question but can you have a class which extends
more than one other class?

That feature is called multiple inheritance, and no, Java does not
support it. One seldomly needs it. But if you want somethink like this,
you have to hack your way around Java's limitation. Use interfaces and
uses-a relations.
 
T

Tor Iver Wilhelmsen

Ian Stanley said:
Appology if silly question but can you have a class which extends more than
one other class?

Not directly but indirectly:

public class YourClass extends Class1 {

private final Class2 class2Impl = new Class2() {
// Class2 "view" of this class, this inner class
// has access to all fields/methods of YourClass
};

public Class2 asClass2() { return class2Impl; }

}

You need to call a method instead of using the object directly, but
you get fairly close to multiple inheritance this way.
 
J

Joe

Hi,
Appology if silly question but can you have a class which extends more than
one other class?
If so what is the correct syntax?
eg class1 needs to extend class2 & class3
thanks
Ian.


Just to step outside of the sctrictly Java-centric answer, you should ask
why you need to inherit two classes. Inheritance means you have an 'is-a'
relationship. For instance, the Dog class would inherit the Mammal class
because a dog is a Mammal. If you find that Dog needs to also inherit
HouseholdPet, maybe HouseholdPet should inherit Mammal and Dog inherits
HouseholdPet instead. It's unusual for a class to *NEED* to inherit two
classes in a well-designed system. Of course, if you're saddled with
someone else's code, sometimes you can't help it.
 
J

Jim McMaster

Joe said:
Just to step outside of the sctrictly Java-centric answer, you should ask
why you need to inherit two classes. Inheritance means you have an 'is-a'
relationship. For instance, the Dog class would inherit the Mammal class
because a dog is a Mammal. If you find that Dog needs to also inherit
HouseholdPet, maybe HouseholdPet should inherit Mammal and Dog inherits
HouseholdPet instead. It's unusual for a class to *NEED* to inherit two
classes in a well-designed system. Of course, if you're saddled with
someone else's code, sometimes you can't help it.

This really is not a good example. What if you have a lizard that is also a
HouseholdPet? It would not make sense for it to inherit from Mammal.

Multiple inheritance has a lot of problems, and people who think they need
it probably should rethink the entire problem. In this case, you probably
need the concept of a "Role" of HouseholdPet, which can be filled by a Dog
or Lizard. You could then also have a "WildPredator" role which could be
filled by either.

A more real-world example would be a University, which has Students and
Employees. Both are members of the class Person, with names, birth date and
so on. You might envision a Student subclass with course schedules and
GPAs, and an Employee subclass with work schedules and salaries. This is
all fine, until you hire a grad student as a teaching assistant. Now which
is he...a Student or an Employee? You could solve this with multiple
inheritance, but can get ugly if both Student and Employee have a
"getSchedule" method. Our TA has both a work schedule and a course
schedule, and how does the object know which one to return?

In my opinion, inheritance should only be used for representing immutable
characteristics of an object, not for things that can come and go during the
life of the system. Defining Student and Employee as roles which are
temporarily filled by a person is a better solution.
 
A

Andrew Thompson

Jim McMaster said:
Joe said:
....
This really is not a good example. What if you have a lizard that is also a
HouseholdPet? It would not make sense for it to inherit from Mammal.

I was going to respond 'lizzards' myself, but you beat me too it..
....In this case, you probably
need the concept of a "Role" of HouseholdPet, which can be filled by a Dog
or Lizard. You could then also have a "WildPredator" role which could be
filled by either.

What about cats?
Not only household pets, but also wild predators!
;-)
 
D

Don Osburn

Ian's example below is very interesting and something I've never thought of
before.

However, regarding multiple inheritance in general, I thought that was what
interfaces were for. Java was designed to enforce a single inheritance
model, but if you needed functionality provided by multiple classes then you
would provide that by utilizing interfaces. (That is why you can only
extend one class, but you can implement as many interfaces as you like.)

I admit, it doesn't seem as "clean" to me as using multiple inheritance in
C++. However, I also feel like my code has gotten much less error-prone
utilizing this model then when I used to work in C++. It seems to take more
work to build multiple interfaces, but I don't feel I my code is near as
difficult to debug as C++ was either. (Then again, there is that whole
pointer thing to consider :) )

Don Osburn
----------------------------------------------------------
 
R

Roedy Green

I admit, it doesn't seem as "clean" to me as using multiple inheritance in
C++.


Part of the reason it is much easier to implement single inheritance.
The JVM can be more streamlined and efficient. See the discussion
about instanceof for example in comp.lang.java.machine.

Another reason is the intractable problem of dealing with clashes in
methods and members from two inheritance streams. C++ has the rather
ugly way of exposing the dirty linen to the outside. Eiffel has a way
of renaming features to tidy them up. There is no obvious simple
solution.
 
R

Roedy Green

Multiple inheritance has a lot of problems, and people who think they need
it probably should rethink the entire problem


I started with C++ too and wondered how the heck you would get along
without multiple inheritance. However, you learn to think in Java,
use interfaces, facades and has-a relationships, and I can recall only
few times when I thought it highly inconvenient not to have it.
 
S

Shawn McDermott

I disagree with this discussion on a symantic point. Interfaces to do
not provide multiple inheritance. You inherit no behavior from the
interface, you are simply forced to implement methods defined in the
interface.

shawn
 
Joined
Oct 29, 2011
Messages
1
Reaction score
0
Multiple inheritance

I have a class which extends JFrame and implements ActionListener
i wanted to add a double click listener to this so i added a new class which extends MouseAdapter now everything works fine till here.
I want to call actionListener from this class when a double click is done but when i call the button by Play.doCLick();
it gives error
cannot find symbol
symbol : variable Play
location: class ActionJList Play.doClick();
please help me
how can i click the button which is in other class
 
Joined
Mar 13, 2012
Messages
1
Reaction score
0
o rly now?

"Joe" <[email protected]> wrote in message
news:[email protected]...
> In article <[email protected]>, (e-mail address removed)
> says...
> > Hi,
> > Appology if silly question but can you have a class which extends more


than
> > one other class?
> > If so what is the correct syntax?
> > eg class1 needs to extend class2 & class3
> > thanks
> > Ian.
>
>
> Just to step outside of the sctrictly Java-centric answer, you should ask
> why you need to inherit two classes. Inheritance means you have an 'is-a'
> relationship. For instance, the Dog class would inherit the Mammal class
> because a dog is a Mammal. If you find that Dog needs to also inherit
> HouseholdPet, maybe HouseholdPet should inherit Mammal and Dog inherits
> HouseholdPet instead. It's unusual for a class to *NEED* to inherit two
> classes in a well-designed system. Of course, if you're saddled with
> someone else's code, sometimes you can't help it.



This really is not a good example. What if you have a lizard that is also a
HouseholdPet? It would not make sense for it to inherit from Mammal.

Multiple inheritance has a lot of problems, and people who think they need
it probably should rethink the entire problem. In this case, you probably
need the concept of a "Role" of HouseholdPet, which can be filled by a Dog
or Lizard. You could then also have a "WildPredator" role which could be
filled by either.

A more real-world example would be a University, which has Students and
Employees. Both are members of the class Person, with names, birth date and
so on. You might envision a Student subclass with course schedules and
GPAs, and an Employee subclass with work schedules and salaries. This is
all fine, until you hire a grad student as a teaching assistant. Now which
is he...a Student or an Employee? You could solve this with multiple
inheritance, but can get ugly if both Student and Employee have a
"getSchedule" method. Our TA has both a work schedule and a course
schedule, and how does the object know which one to return?

In my opinion, inheritance should only be used for representing immutable
characteristics of an object, not for things that can come and go during the
life of the system. Defining Student and Employee as roles which are
temporarily filled by a person is a better solution.
--
Jim McMaster
mailto: (e-mail address removed)

You know, you could have used a simple example of where it does work. For instance: a class called Fabric, a class that extends it called Clothes, and a class that extends Clothes called Jeans. All Jeans are Clothes, all Clothes are made of Fabric, and of course all Jeans are made of Fabric (by transitivity). There, simple example of when you could legitimately inherit from multiple classes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top