Use of Java [Interfaces / Abstract classes] [duplicate]

You can think of an interface as a “contract”. You are defining a set of methods that classes which implement this interface must implement.

An abstract class, on the other hand, is used when you have some code that could be common to all the child classes you want to implement. So you might have an abstract class called Shape that has some common code, and in your derived classes (Circle, Square, etc.) you could have the code that is specific to those shapes (getArea would be an example). But something like color might be common to all shapes, so you could put a getColor method in your Shape abstract class.

And you can combine the two ideas. You can have abstract classes which implement interfaces, and this gives you the best of both worlds.

These concepts are used over and over again in OO, so it’s important to understand them. You seem to be well on your way :).

So if your zombie class has some common behavior that applies to all types of zombies, it sounds like a good candidate to be an abstract class. You could also consider creating an interface (maybe a GameCharacter interface) if you have other characters in your game (maybe UndeadMice or something :)). Then your Zombie abstract class and UndeadMouse abstract class would implement the GameCharacter interface.

Leave a Comment