Jump to content

he supShould I make one method in ter class or 2 in the sub classes?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
johnsonk

johnsonk

    Newbie

  • Members
  • PipPip
  • 17 posts
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.

#2
johnsonk

johnsonk

    Newbie

  • Members
  • PipPip
  • 17 posts
sorry the title should be
Should I make one method in the super class or 2 in the sub classes?

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
johnsonk

johnsonk

    Newbie

  • Members
  • PipPip
  • 17 posts
Thanks for the reply WingedPanther.

Am I right in saying that would mean that Fish would be an interface?

regards,
Kyle

#5
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#6
johnsonk

johnsonk

    Newbie

  • Members
  • PipPip
  • 17 posts
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.