Introduction to abstraction
You have learned in this chapter that OOP creates an abstract view of the things that we interact with so that we can use them in code. However, you can apply an additional level of abstraction to your classes in C# with abstract classes.
Let’s use our existing animal example to explain the concept. We now know that the Animal
class doesn’t have enough members to describe all animals, but we also know that some members from derived classes of animals will not make sense in other derived classes. For example, a Cat
or Dog
class shouldn’t need a property for gills, like a Lizard
class would, but all three animal types will have an age. It seems that the Animal
class won’t ever be used by itself, only as a base class. It can be inverted into an abstract class so that the following apply:
- The class will never be instantiated, since we know it’s best used as a base class and should only be used for that purpose ...