Best way to do parent/child events

This my be silly but i need to know.

I have many animals that are children of the parent animal. Is better to add functions to each animal sub class as needed or make the parent a super animal that has all the functions for every animal?

Hey br_chichn,

Generally, you would want to add generic functions in the parent and get more specific as you add more sub classes. These would be things that all of your sub classes would likely use. For example, all animals need to eat, move, and search. This could be further broken down by different types of animals.

AnimalClass (Functions: eat, search, and mate)

> Aquatic (Swim)
> > Shark (Bite)
> > Whale(Breach)

>Terrestrial (walk/Run)
> > Cat (Claw)
> > > Lion (Roar)

> Aerial (Fly/Glide)
> > Bird (lay egg)
> > > Falcon (Dive Bomb)

This structure allows for better organization and generally allows for all of the same functionality. For example the “Falcon” class in this example can use the following functions (eat, search, mate, Fly/Glide, lay egg, and Dive bomb).

I hope that this helps answer your question and show you as to why would would want to use the method I have described.

Make it a great day

Ok. That makes total since. Let say i add a “flying fish” animal. It can swim and fly/glide. It would be in a fish sub class but it would not have access to the glide function. Or am I still not getting the concept?

Other than organization reasons, is there any other reason why not to make a super animal that has all functions? Then the animals call functions as they need?

I’m trying to figured out how in want to layout this project before I get to deep in it.

Hey,

With all projects you will need to have an idea for how you are going to structure your classes and what they are going to be capable of before creating your bases. The example above was not conceived with fly fish in mind. However, if I ran into this issue later in a project I may create a new sub class that handles this and just eat the time sink in copying over my logic.

Yes, making a super class could have it’s benefits but it will be inherently less efficient. It would be like carrying the table and all of its dishes to the sink when all you wanted was to wash a plate (repeat for every dish on the table).

Good luck with your project

Make it a great day