What are abstract classes?
If you’ve been using or learning OOP for some time, you would’ve heard the words abstract classes being thrown about. I have found that those who are self-taught generally miss or skip the sections that focus on all aspects of abstraction. I think this is mainly down to its use case shining when working on large codebases and gigantic teams.
Unlike regular classes, abstract classes can’t be instantiated, although they are allowed to contain concrete methods and properties. Classes that inherit from an abstract class must implement all abstract members.
Something to consider is that you don’t always need abstract classes; sometimes, interfaces will be the better option. The general rule of thumb is that if in your abstract class you’ve just created has no state, it should be an interface. C# has a special abstract
keyword for creating abstract classes.
Let’s look at a quick example.
public abstract class Animal
{
private bool isHungry = false;
public abstract void Eat();
}
public class Cow : Animal
{
public override void Eat()
{
// Implement how the Cow will eat its food.
}
}
I am creating these articles for my Advanced OOP roadmap for GameDev, which you can visit here over on roadmap.sh