Abstract Base Class¶
The role of abstract base classes is to help group actual classes together based on common behaviour or implementation. The base class reveals a common hierarchical interface to the base classes. At the same time the implementation of the interface functions is hidden in the subclasses i.e. the concrete classes according to their specific behaviour. All descendants of abstract base classes share the same interface and thus the same behaviour which makes calling them through a common name possible in the program. An abstract base class can also implement the common functionality of its decendants already in the base class. The base class is abstract if even one of the services on the interface is abstract i.e. left unimplemented on the level of the base class. As the base class does not provide a full implementation for its interface it cannot be instantiated as an object. It’s purpose is to act as a base class. The interface of an abstract base class can contain:
abstract interface functions that the conrete classes fully implement
interface functions providing the common part of the service provided to the subclasses
interface functions with a full implementation
The base class is abstract until all abstract services in the interface have been given an implementation.
Let’s take a closer look at the class hierarchy abowe. from the point of view of abstract base classes. An Organism is a base class representing all organisms. It defines that all organisms in the program reproduce is a common way when viewed from the outside. Organism does not define in any way how reproduction gets implemented. As objects can be handled as objects of their base class each organism can thus be told to reproduce in the program without needing to know details on which animal the object actually is. Human and Hen still reproduce very differently. Therefore reproduction has been defined as abstract for organisms: reproduction is implemented in the concrete classes in a suitable way to them. Similarly on the next level in the hierarchy an abstract base class Bird defines two eatures common to all birds: laying eggs (layEgg) and singing (sing). Singing is abstract fro birds as each bird defines it according to its own singing voice. Laying eggs however has an implementation on the level of the base class. Through inheritance birds can naturally change the functionality of egg laying to their style but the implementation common to all is already given on the level of the base class.
Let us then pay attention to the egg laying of birds and the abstract service common to all animals: moving. Even if the Platypus is a mammal it lays eggs which it hatches in its nest to reproduce. This is of course behaviour shared with birds. We cannot still move the platypus under birds in the hierarchy as it does not otherwise fullfill the common functionality of birds. Another similar difficult is with Fungi: SlimeMold is special in that it can also move but our hierarchy defines moving as a trait common to animals. (SlimeMold acts here also as an example of how difficult categorizing is overall and how the class hierarchy doesn’t always match the real world it depicts one to one: slimemold isn’t actually a fungus even if it is categorized as one. It is not an animal either so for the sake of example we’ll go with it.) The solution to these kinds of situations wheren all functionality cannot be grouped under one hierarchy is found in interface classes.
Abstract Base Classes (duration 12:03)