Abstract class. When you should use them and why

Abstract is similar to an Interface, but not quite

An abstract class (often called a base class) has similarities to a interfaces if you are already familiar with them. One of the bigger differences are the following:

  • Multiple interfaces can be inherited in a single class, only one abstract class can be inherited.
  • Interfaces only have signatures to make requirements be implemented when inherited, abstract classes can have actual implementations that can be used in classes that inherit.

One of the biggest advantages to abstract classes is that last bullet. abstract methods can have actual implementations for their methods along with abstract methods that are only signatures and must be implemented by the derived class. Let’s look at a very common example of abstract implementation and dissect it a bit.

The Abstract class example

Let’s pretend we are being asked to build a utility library for a zoo so that different applications can reference your code and add new animals as needed. The following requirements must be met:

  • Common properties any animal should have should be added to the class (name, native land, family etc..)
  • Common capabilities should be added that can be called and overridden.
  •  Provide the capability to call a method and return all it’s properties and types.

The first thing you will notice is that the class is decorated with the abstract keyword. This tells the compiler that you will not be instantiating the class anywhere and will therefore be placed on a more efficient place in the heap when the code is run. This will make your code run slightly more efficient and even more importantly, keep other developers from messing around with the utility class functionality. To get even more control over your class, use structs. Structs are most commonly used in small property blocks that are all you need. A good example are the coordinates  of a Rectangle.

Next you will see that we have added 3 public properties. The first one, Name also has the abstract keyword added to it. As you will see once we inherit the class, the derived class will be forced to override the Name field with it’s own implementation. We are doing this because we want the developer implementing this class to at the very list give it a name.

Abstract Class Method implementations

The following methods have been added to meet the next requirements which were to have a method that was to be required by every derived class, one to could be called or overridden, and the capability to call one method that returns the values of that type.

Before we start though, lets define a class that will inherit our AnimalBase class. This way, as we go, I can show you the definition and implementation of each of the abstract classes capabilities.

Now, as soon as you add this code, you should see some errors that look something like this:
‘Kangaroo’ does not implement inherited abstract member ‘AnimalBase.Name.get’
‘Kangaroo’ does not implement inherited abstract member ‘AnimalBase.Name.set’

This is fine, and there is a good reason for this. Remember when I said that the Name property had the abstract keyword on it? Well that made the Name property required. So lets implement it :).

Now, there is one key piece here and that is the use of the override keyword. The override keyword is exactly what it sounds like. It overrides whatever the abstract class is exposing. This means that when we call Kangaroo.Name, it will return Kangaroo. The real takeaway here is that the developer who inherited from AnimalBase was required to override the Name property. This can be important when you have some property that you rely on within your base class or want to make sure every inherited class has it’s own version of.

Abstract Methods

The first is an abstract method. This method being abstract will enforce it be implemented when a class inherits it.

Now, similar to the property issue, after adding this to your AnimalBase class, you will get the following error.

‘Kangaroo’ does not implement inherited abstract member ‘AnimalBase.MakeNoise()’

Sounds good, so let’s do what the compiler wants.

Using the override keyword allows for the implementation of our own version of the MakeNoise() method. I have no idea what a Kangaroo says, so Squeak is good enough :).

Virtual Methods

Virtual methods are the best of both worlds. This provides us with a way to have a default implementation AND give the option to override it as well.

Here is how virtual methods are written.

The virtual keyword gives the capability of having the default method above, and then below, I will show you the Kangaroo implementation. We are also setting the Acceleration property so it can be called later if we want.

A few interesting things here. First, we are overriding the Run method instead of doing the increase by 5, the kangaroo multiplies by 5 because Kangaroos are like super fast :).

The other cool thing is this base.Run(speed);base means when you are done doing what your done, run the abstract version.

By creating a Kangaroo and pass in a speed of 3, we should get 15. Here is what it will look like:

If everything goes right, here is what you will get:

result of the code
result of the code

Just to put it all together…





Happy Coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *