Abstract Class

What is the usage of abstract class exactly? I do not get it. What happens if we don’t use it? Can someone give me an example when we don’t use it and it causes a problem?

An abstract class gives us a common base for a set of classes without requiring us to implement all of the functionality of that class.

Without the class being abstract, we cannot declare abstract methods, meaning, for example, if State wasn’t abstract, then it would need to implement a definition for each of the Enter(), Tick() and Exit() methods.

Now this is perfectly fine, if you wish. You could make State a non-abstract class and declare these methods a virtual instead of abstract providing implementations. All being abstract really does is prevent an otherwise not quite baked class from being instantiated. As creating a new State() doesn’t make much sense (as it wouldn’t actually do anything), it’s unlikely you’d try to create a new one anyways.

1 Like

@Brian_Trotter
The question was actually posted in relation to the BaseAction being made abstract, so the information about states and abstract methods might be somewhat confusing for the OP… :wink:

In the current state of the project, the BaseAction is simply concentrating the logic for accessing the Unit component and the declaration of bool isActive to give a common ground for all actions within the project. (Actually the SpinAction doesn’t really make use of the reference to the unit and just implies it’s running as a component on the unit…)

In my own project I already moved one step further and put the guarding if(i!isActive) return; into an Update() method in the BaseAction

Sooner or later there will probably be methods defined in the base class that have no content at all (the abstract methods, that Brian already mentioned), which would require the inheriting classes to implement them.

Up to that point it just doesn’t really make sense to have a BaseAction added to the unit. The base class has no action that could be called and do something. Since its purpose is to be inherited by the real actions it makes sense already to prohibit creating a BaseAction instance.
Once you have abstract methods, you couldn’t really create an instance anymore since the abstract methods are only declared but have no code (similar to what an interface does, but an interface has no code at all). (I believe you also can’t have an abstract method in a class that isn’t also abstract itself…)

TL;DR: It’s preparing the BaseAction class for things yet to come in the following lections.

2 Likes

Privacy & Terms