getting round multiple inheritance

C

Colin Hemmings

Hi there I'm wondering if anyone can give me some advice. I have a
program using RMI, and now I want to make some of the objects that are
in it use observable. As java doesnt allow multiple inheritance, I can't
use Observable as well as RMI.

I've read that the best way round this is to create a new class which
has the functionality of the Observable class, but I'm unsure exactly
how to go about this, what to include in it, etc.

could someone offer so advice on how i could go about sorting this?

thanks
 
R

Roedy Green

Hi there I'm wondering if anyone can give me some advice. I have a
program using RMI, and now I want to make some of the objects that are
in it use observable. As java doesnt allow multiple inheritance, I can't
use Observable as well as RMI.

You object can have an Observable field. The only other way I know of
solving that is to extend and Observable and implement the necessary
additional inferfaces, perhaps with the facade design pattern.
 
T

Thomas Hawtin

Colin said:
Hi there I'm wondering if anyone can give me some advice. I have a
program using RMI, and now I want to make some of the objects that are
in it use observable. As java doesnt allow multiple inheritance, I can't
use Observable as well as RMI.

I've read that the best way round this is to create a new class which
has the functionality of the Observable class, but I'm unsure exactly
how to go about this, what to include in it, etc.

Ditch Observable. It never was a very good class.

Presumably lots of JDK 1.0 code extended it. So Observable became part
of that code's interface. That makes it difficult to deprecate.

The all new Java 1.1 way is to use beans-style listeners.
javax.swing.EventListenerList is useful to manage the listeners.
javax.swing.ChangeListener is a rough equivalent to Observer. The only
significant problem with these two types is that they are in the wrong
package (they should have been in java.beans, or perhaps the java.util
dumping ground).

Tom Hawtin
 
T

tom fredriksen

Colin said:
Hi there I'm wondering if anyone can give me some advice. I have a
program using RMI, and now I want to make some of the objects that are
in it use observable. As java doesnt allow multiple inheritance, I can't
use Observable as well as RMI.

In Java you use interfaces as a sort of replacement for multiple
inheritance. When designing an app your class hierarchy, the inheritance
chain, would be focused on the most core functionality of your classes,
while you would use interfaces for the extra features.

F.ex lets say you are designing an 3D modeling program, then your
classes core tasks would be about 3d concepts, while the non core
functionality of the class would be covered by interfaces. So you could
create a class hierarchy with a root class which inherits from RMI.

E.g. RMI -> Shape -> Triangle
Now triangle would perhaps implement the Observable interface so you
could f.ex.check for collisions.
I've read that the best way round this is to create a new class which
has the functionality of the Observable class, but I'm unsure exactly
how to go about this, what to include in it, etc.

I would use the Observable design pattern, not the class implementation
available in the jdk. You can find a description of it on the net.
Or you could read about it in the book "Head First Design Patterns",
which is an excellent book for beginners about application design.

/tom
 
T

Timo Stamm

tom said:
In Java you use interfaces as a sort of replacement for multiple
inheritance.

I think I understand what you mean, but I don't quite agree with your
explanations:

Inheritance is a powerful tool for reuse. Interfaces just seperate
declarations from implementations. Interfaces can actually use multiple
inheritance (interface X extends A, B), but I don't think that they can
be seen as a /replacement/ for multiple inheritance, because the
implementation is not inherited.

It is possible to use something like multiple inheritance in java,
though. You can use the composite pattern and delegation (like defined
by the GoF). There is java.lang.Proxy, which already does most of the
dirty work.

But it doesn't solve all problems, and it is still much more verbose in
java than in a language with built-in support for multiple inheritance.

When designing an app your class hierarchy, the inheritance
chain, would be focused on the most core functionality of your classes,
while you would use interfaces for the extra features.

F.ex lets say you are designing an 3D modeling program, then your
classes core tasks would be about 3d concepts, while the non core
functionality of the class would be covered by interfaces. So you could
create a class hierarchy with a root class which inherits from RMI.

Are we talking about Remote Method Invocation? Why should a 3D library
have anything to to with RMI, let alone, extend its core classes from a
RMI class?

What if you need another technology than RMI in the next 3D app? You
would either have to make radical changes to the library or rewrite it.


Timo
 
T

tom fredriksen

Timo said:
I think I understand what you mean, but I don't quite agree with your
explanations:
Inheritance is a powerful tool for reuse. Interfaces just seperate
declarations from implementations. Interfaces can actually use multiple
inheritance (interface X extends A, B), but I don't think that they can
be seen as a /replacement/ for multiple inheritance, because the
implementation is not inherited.

I sort of technically agree with you. Its just a matter of point of view.

Are we talking about Remote Method Invocation? Why should a 3D library
have anything to to with RMI, let alone, extend its core classes from a
RMI class?

What if you need another technology than RMI in the next 3D app? You
would either have to make radical changes to the library or rewrite it.

It was just an example made up to fit the question, but not a very good
one. The point was that you can for some things make it the root class
of the hierarchy if it is needed by all subclasses, that way you can at
least get one implementation in there for you hierarchy, a sort of half
multiple inheritance method. (The last part is bollocks, its jsut my way
of viewing it)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top