Designing Enemies using Abstract Classes

it’s common for your enemies to share traits such as Health and Speed. Simple inheritance can be used across your enemy classes to help streamline things!

This is where abstract classes shine. They focus on the accountability of an interface while simultaneously allowing for the flexibility of traditional class inheritance.

In this example, we have set up a simple class with health and speed variables along with an Attack method. We can adjust the health and speed between enemy types as well as call Attack from any class that inherits from enemy

If we want to make sure that all classes that inherit from Enemy implement the Attack method, we use the keyword “abstract”. We define the class itself as abstract as well as any methods (without definitions) we want to be utilized:

This give the flexibility of traditional inheritance while making sure that child classes implement specific functionality.


Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store