Hello,
I'm quite new to programming and working on a small project to familiarise myself with inheritance.
My project is on a simulation of a simple ocean world. There are two types of creature Plankton and Fish. Fish have two subtypes – shark and sardine.
The program holds a collection of creatures. The idea is that Sharks can eat Sardines and Sardines can eat Plankton, this would delete the corresponding creature from the collection.
In practice would I have a single “eat” method in class Fish that accepts type creature, or "eat" methods in the sub classes that accept type sardine or plankton.
he supShould I make one method in ter class or 2 in the sub classes?
Started by johnsonk, Oct 26 2009 01:26 PM
5 replies to this topic
#1
Posted 26 October 2009 - 01:26 PM
|
|
|
#2
Posted 26 October 2009 - 01:47 PM
sorry the title should be
Should I make one method in the super class or 2 in the sub classes?
Should I make one method in the super class or 2 in the sub classes?
#3
Posted 26 October 2009 - 02:37 PM
Normally, I would have eat as a method of fish that is implemented by shark and sardine. Plankton shouldn't have to know that it can be eaten.
#4
Posted 26 October 2009 - 03:35 PM
Thanks for the reply WingedPanther.
Am I right in saying that would mean that Fish would be an interface?
regards,
Kyle
Am I right in saying that would mean that Fish would be an interface?
regards,
Kyle
#5
Posted 26 October 2009 - 04:04 PM
It depends on how you build Fish, and in what language. For Java, most likely. For C++, maybe not (as interfaces are just standard inheritance of parent classes that have no data members).
#6
Posted 27 October 2009 - 12:28 AM
Yeah the program would be in java.
The only problem is that I wouldn't want an object of type sardine to be able accept type shark in its eat method, being a creature.
The only problem is that I wouldn't want an object of type sardine to be able accept type shark in its eat method, being a creature.


Sign In
Create Account


Back to top









