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.
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.