Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Interfaces instead of multiple inheritance?

843789Jul 10 2009 — edited Jul 11 2009
I've read that "The Java programming language does not permit multiple inheritance , but interfaces provide an alternative."

But I also read contradictory information-There are no method bodies in an interface.

Java interfaces only contain empty methods? Apparently, if I want to share a method among classes, I have to re-write the methods in each class that implements the interface. That doesn't seem at all like multiple inheritance. Am I missing something?

It seems that I will have to cut and paste the implementation code from one class to another, and if I change the methods, I have to cut and paste it all over again.

I've read that interfaces save a lot of time re-writing methods, but how?

Does this really provide the same capabilities as multiple inheritance, or am I missing something?

Thanks,
Pat

Comments

800308
jverd wrote:
Demi Moore in Ghost. Yum!
That might be the most intelligent thing you've ever said ;-)
800308
jverd wrote:
uh-oh, what do we do here?
A: http://www.fakecrap.com/images/jokes/bang_gun_with_flag.jpg

Hey, I think I'm hilarious ;-)
800308
jverd wrote:
Pat-2112 wrote:
I've read that "The Java programming language does not permit multiple inheritance , but interfaces provide an alternative."
....
....
Yup. You're missing the point of inheritance, and the fact that delegation allows you to use an implementation defined in one class in another class.
... nice little example of a Mulitple Inheritance collision: Cowboy.draw() + Artist.draw() = CowboyArtist.draw() {?WTF?}  ...
The draw method is not relevant to this particular question. It's an example of one of the problems with MI, and I just included it since it usually comes up int these discussions anyway. Ride and sculpt demonstrate the point about delegation.
Seriously this time...

YES BUT... I do think Pat has a point. Don't you? Do programmers have the right to expect the language to provide assistance with common problems? Aren't Mulitple Inheritance (MI) scenarios a fairly common problem? And could/should we expect a "good OO language" to offer "a common solution" to this problem?

I'm interested... What "patterns" have you (and others) cooked-up to facilitate many interface-implementations sharing a common concrete-implementation of a given method, or set of methods?

I don't think this is "the right approach", but I've been doing it anyway... I push-out the implementation of method(s)-in-common to static method(s) in abstract class(es). Each interface-implementation then delegates to those static concrete-implementations.

This is "fast and loose" because it doesn't constrain the implementation to having to extend an abstract "adapter" class in order to use the "common code", nor does it specify the "type"... so "the type" is kept nicely seperated from "the implementation".

In your opinion, Is this approach spot on? Or maybe it constitutes grounds for immediate dismissal? Or is it somewhere in between?

Cheers. Keith.
843789
Aren't Mulitple Inheritance (MI) scenarios a fairly common problem?
I find them quite rare, to the point that I can't recall if I ever wanted to do multiple inheritance. Maybe it's like goto: when you don't have it, you don't miss it.

What kinds of real world needs of multiple inheritance do you keep running into? Not twisted artificial examples tailored to need it; I can conjure up a situation that needs goto, and keep modifying the made up premise whenever you show a way to do the program without goto; real examples from real programming tasks.
843789
What kinds of real world needs of multiple inheritance do you keep running into? Not twisted artificial examples tailored to need it; I can conjure up a situation that needs goto, and keep modifying the made up premise whenever you show a way to do the program without goto; real examples from real programming tasks.
Lots of things. Say I wanted to create a scene where a balloon flies above a field of balls. I decide that Balls and Balllons share common attributes (shape, size, etc) and a actions (canInflate, danDeflate, canBouce, etc).
In my world, I also have Airplanes. I might want to abstract Airplanes and Ballons to ThingsThatFly that have common attributes like altitude, velicity, attitude, etc and actions like takeOff, land, turn, climb, descend, and so on.

Now I am a newb to Java (I am, really), largely self taught or taking a class, doesn't matter, and alot of what is being said about inheritance is that a big benefit is code re-use something like this "If you write a method in a parent class, and don't override it in a child class, then you don't have to test it, etc)." Well that sounds great.

So in my example, I have a balloon that is a ball and is a ThingsThatFlies and to newbs learning about inheritance, examples like this scream for multiple inheritance but it becomes the programmers responsibility to keep the name spaces separate to avoid colliding method and attribute names.

Keiths rant here http://forums.sun.com/thread.jspa?messageID=10757613#10757613 runs counter to what newbs to Java are being taught.
843789
mfratto wrote:
What kinds of real world needs of multiple inheritance do you keep running into? Not twisted artificial examples tailored to need it; I can conjure up a situation that needs goto, and keep modifying the made up premise whenever you show a way to do the program without goto; real examples from real programming tasks.
Lots of things. Say I wanted to create a scene where a balloon flies above a field of balls. I decide that Balls and Balllons share common attributes (shape, size, etc) and a actions (canInflate, danDeflate, canBouce, etc).
Interfaces solve that rather neatly. Flyable and Inflatable interfaces, for instance.
843789
Interfaces solve that rather neatly. Flyable and Inflatable interfaces, for instance.
Of course they do. :) The more I begin to grok Interfaces, the more sense they make, but I am only an egg.

Interfaces offer a level of indirection, but they are often not explained well in books or the explanation comes later in an advanced topic while inheritance is mentioned right away. :)
843789
Lots of things. Say I wanted to create a scene where a balloon flies above a field of balls. I decide that Balls and Balllons share common attributes (shape, size, etc) and a actions (canInflate, danDeflate, canBouce, etc).
So were you writing a web application? A graphical simulation? A JDBC catalogue of different kinds of balloons? A web shop for balloon affecionadoes?

Is this an imaginary problem or a real programming task that you are facing? Please do not be afraid to describe concrete real programming challenges that you are facing in the real world.

Imaginary problems are not useful. With imaginary bull$h1t problems I can conclusively prove that the goto statement is absolutely the best thing after sliced bread.
YoungWinston
Pat-2112 wrote:
Java interfaces only contain empty methods? Apparently, if I want to share a method among classes, I have to re-write the methods in each class that implements the interface.
...
It seems that I will have to cut and paste the implementation code from one class to another, and if I change the methods, I have to cut and paste it all over again.
To my mind, this is only remaining argument for capital punishment.
Personally, I reckon Java development APIs should disable text copying of any kind, so that people are forced to come up with other solutions.

Just one of these is a cooperative class, a bit like 'Arrays' or 'Collections', which contains implementations for those methods that you want to share among your class hierarchy. You need to use a bit more care if you want them to be publicly available, but you still only have to write 'em once.

Winston
1 - 9
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Aug 8 2009
Added on Jul 10 2009
11 comments
152 views