Interface and Abstract Classes- Game Dev Series 107

Conclusion of difference between using interface and abstract classes.

We have created 2 modular scripts- Enemy, which is an abstract class; and IDamageable, an interface. Both of them look quite similar but still have some difference.
In this article, we will make a clear mind of difference between using these two.

Difference of using

This would be the biggest difference.
A class can only inheritance from 1 abstract class and can implement multiple interface.

In this Skeleton class, it inheritance the base from Enemy abstract, and also implement 3 interface.
Now I would explain about these 2 type of script.

Abstract class

An abstract class can create any variables or methods to it, and derived classes can decide whether using these or not.

As in our Enemy abstract class, we create common variables for all the enemies.

When a new class inheritance from Enemy, it is allowed to use these variables, and of course it must be “public” or “protected”.
That allows us to create multiple type of enemies by sharing single core system.

When inheritance a method from abstract class, you can implement parts or all from that method depend on the type: virtual or abstract.

A virtual method means the derived class can decide to implement what it needs from that method.

When a derived class needs to add some code from a virtual method, use override to implement that method, and also can choose if it needs the base code from that method by using base.”methodname”().

For those virtual classes that have not override from derived class, would be all processed by default.

An abstract method can not contain definitions.
However, it must be implemented to derived classes with override.

Interface

As we mentioned in the previous article, interface is just like a contract. It would force the class that implemented from it that must implement all methods. And similar to abstract class, the methods in interface can only be declared, can not contain definitions. There is also no variables in it, only properties.

When implement from an interface, simply type in the name of interface after inheritance class by using comma.

And the implemented class must contain all members from that interface.

That makes interface could be used on specific function that could be implemented to different type of class.
For example, an interface called ITalkable might be implemented to human characters, a phone, a goblin, or even a possessed stone(Some example that might be used in a RPG game).

Conclusion

Although these 2 type of scripts have similar way of using. The meaning of their implement is unique.
Depend on what function you need to build in your game, and choose wisely(or said, use them wisely) is quite important(to me).

--

--

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
S.J. Jason Liu

S.J. Jason Liu

99 Followers

A passionate gamer whose goal is to work in video game development.