Reader small image

You're reading from  SDL Game Development

Product typeBook
Published inJun 2013
Reading LevelBeginner
PublisherPackt
ISBN-139781849696821
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Shaun Mitchell
Shaun Mitchell
author image
Shaun Mitchell

Shaun Mitchell is a developer at a high profile online gaming company. He holds a BSc in Game Programming and Development from Qantm College / SAE Institute London. Shaun is also a moderator and active member of the <dream.in.code> programming community.
Read more about Shaun Mitchell

Right arrow

Should we always use inheritance?


Inheritance and polymorphism are both very useful and really show off the power of object-oriented programming. However, in some circumstances, inheritance can cause more problems than it solves, and therefore, we should bear in mind a few rules of thumb when deciding whether or not to use it.

Could the same thing be achieved with a simpler solution?

Let's say we want to make a more powerful Enemy object; it will have the same behavior a regular Enemy object will have but with more health. One possible solution would be to derive a new class PowerEnemy from Enemy and give it double health. In this solution the new class will seem extremely sparse; it will use the functionality from Enemy but with one different value. An easier solution would be to have a way to set the health of an Enemy class, whether through an accessor or in the constructor. Inheritance isn't needed at all.

Derived classes should model the "is a" relationship

When deriving a class, it is...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
SDL Game Development
Published in: Jun 2013Publisher: PacktISBN-13: 9781849696821

Author (1)

author image
Shaun Mitchell

Shaun Mitchell is a developer at a high profile online gaming company. He holds a BSc in Game Programming and Development from Qantm College / SAE Institute London. Shaun is also a moderator and active member of the <dream.in.code> programming community.
Read more about Shaun Mitchell