Nerd For Tech
Published in

Nerd For Tech

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.

Virtual Methods

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.

Abstract Classes

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.

Summary

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.

--

--

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