Abstract Classes in Unity
In my last article, I discussed how to use class inheritance. In this article, I will be going over abstract classes.
To start let's talk about virtual methods. A virtual method will allow you to create a method that can be inherited by multiple classes and have its own implementation of that method.
This is done by adding the keyword virtual to a method.
When the class is inherited using the keyword override you will be able to add different functionality to the virtual method.
You can use the old implementation by using the base keyword.
An abstract class allows you to force a class to inherit an attribute. To make a class an abstract class add the keyword abstract to the class declaration.
Create a method with the keyword abstract and the method will be forced onto any class inheriting it.
Now you will be forced to inherit the method.
Virtual methods are used as a way to allow a class to implement its own functionality for an inherited method. Abstract classes are used to force inheritance to the classes that are using the abstract class.