Java: Inheritance, Extending, and Composition Problem

Relli

Weaksauce
Joined
Jan 16, 2001
Messages
98
I have an assignment that I'm supposed to eliminate "extends" from the code and find another way to do it.

The code given has a mammal class that is extented by a herbivor and a carnivor class. The herbivor is extended by a cow and the carnivor is extended by a cat. At each level there is a simple method that prints a simple statement. There is also a tester class that just creates instances of the cat and cow and calls methods that are supposed to be inherited.

I have got it this far using composition:
In the test class I can create a new cat: Cat c = new Cat;
and call methods that are "inherited" through composition.

My problem is the teachers test class creates a new cat like this:
Mammal m = new Cat;
which works when every class extends the class above it.

What should I be searching for to fix this? He suggested aggrigation, but that didn't pull up anything. It did point me to composition, which I have tried, but it isn't quite working.

Any suggestions would be appreciated.
 
::peeve::

I hate these type of questions in school:

"here's the right way to do it, the way that will work 100% of the time and the way that every manager in the world will expect you to do it, but just for the hell of it let's learn the wrong way to do things and screw you up later in life"
 
Implements is what you want. Implementing an interface is completely different from extending IMHO.

And in this case if Mammal can be an interface my guess would be this is what he is looking for. Although it seems like kind of a silly assignment. Why not just give them an assignment that requires a class that needs methods from multiple interfaces.

class Human implements Mammal, ToolUser

since you can't have

class Human extends Mammal, ToolUser

Something like that would better illustrate the reason for using and interface over a base class
 
That is what I was thinking. I just wasn't sure on how to use extends I guess. I finished it up pretty quick this morning. I think I was just up to late.

Thanks for the help.
 
your teacher is a fucking moron. as much as java sucks, if you have to use it you may as well use it to its full, object oriented, potential

sorry i know that doesn't help. just agreeing with Fryguy8
 
as much as java sucks,

Not to start a flame or anything I thought I would just point out that in my experience anyone who thinks a language sucks hasn't learned it very well. Any language, yes even fortran and cobol, that has reached the level of maturity of java has spent alot of time in design and construction. I could say that perl sucks because as far as I can tell it's just line noise. But that just shows my ignorance of the langauge. To an experienced perl programmer it must at least look like CRC checked line noise :p
 
This problem deals with OO design issues. The lesson here is how not to abuse inheritence, which usually results in deep inheritence tree and brittle code.
Instead of using inheritance to "specialize" a behavior, use composition. Meaning encapsulate the varying behavior in another class, and have the original class contain an object of the new class.
For example, you have Mammals. Cow and Cat can inherit from Mammal (common traits).
Herbivor and carnivor are eating habits (difference is here). So you can have a class EatingHabit, and have Herbivor and Carnivor inherit from EatingHabit.
Now Cow will contain an object of Herbivor and Cat will contain an object of Carnivor.
Mammal probably has a method called eat(). EatingHabit will need a method called eat(). Mammal::eat() calls EatingHabit::eat().
 
*shrug* didn't bother, I've lived it. I have been a full time Java programmer for 5 years. Working on some VERY large projects (over 30k classes). Over the years I would probably have listed all of the same problems as they do.

I did take a look at the sites you mentioned, they have been a very interesting read.

I really liked the way the first one breaks Java up like this.
A language
An enormous class library
A virtual machine
A security model

So I will use it here.

Language:
From the first article the problems with the Language itself have alot to do with his background in C. I personally have no problem living without inline/macros anymore. Who needs local functions when you can have local anonymous classes :). Although EVERYTHING he says about simple types is spot on.

Class Library:
The class library has alot of inconsistencies. Of course don't get me started on STL and MFC.

Virtual Machine:
I am really looking forward to the Jakarta Harmony project (OpenSource VM). There is a reason Sun calls their JVM a "Reference Architecture"

Security Model:
Everything he says is very true.

What I am really "arguing" for here is that the language itself does not suck. And I'll quote the author of the first article.
Java-the-language is, overall, a very good thing, and works well.

I hated Java when I first started working with it. I thought it was slow. That it took away all the power I had with C/C++. Macros were almost impossible to live without. Putting an entire class in only one .java file seemed stupid. But as I got used to the model I began to see why things are the way they are.

Sorry, not trying to hijack the thread for a soapbox trip. Just thought I would voice a counter-opinion

Edit: I have been reading some more of the 2nd article. I keep getting upset over the "obvious" things he is pointing out. I have to keep reminding myself this guy actually knows what he is talking about, but he's actually had someone ask him about this in the past. Some of the stuff just makes me sad. I mean
"".compareTo( null ) raises a java.lang.NullPointerException.
Well DUH. your passing a null value to something that expects a non-null value. I can't imagine why it would complain.
 
Thanks for the help for the couple that helped. I didn't mean to start a java's worth argument.

tim_m, I just wanted to point out that extending a class is less of an object oriented way to do something than using composition. It may be more of a Java way, but I think it is less of a pure object oriented way. The class I was taking was OO design and analysis. Since we use Java in the class I think he was trying to show a more OO design method and a way to produce less interdependent code.
 
Sorry to get off topic. Time to return.

Extending a class is just as much of an OO way of doing things as composition. You just have to make sure you use it right.

Extending and Implementing have to do with a "Is-A" relationship
Composition has to do with a "Has-A" relationship

To follow up on GlimmerMan's excellent comments
a Cat "Is-A" Mammal but a Cat "Has-A" Eating Habit.

A Mammal will have an eating habit, but not knowing what a Mammals eating habit may be we can't include that code in the Mammal.

A bad use would be to have a Cat extend an EatingHabit field because it's not a true "Is-A" relationship, comments about "You are what you Eat" not withstanding.

Not as bad, but could still trip you up would be to have a Cat contain a Mammal reference, but not extend it. If somebody were to add a getter for the Mammal being an invertebrate or not then in order to find this out at the Cat level you would have to write methods for it. Of course this is probably moot because you couldn't have a Cat referenced as a Mammal anyway.

Hope this is a more constructive post
 
Fryguy8 said:
Do some research before you argue with people about whether a language sucks or not.
You do realize that there are dozens of web pages out there like this for every language ever created, right? I don't think that's enough to conclude objectively that "Java sucks".
 
Back
Top