Abstract Constructs
Last updated
Was this helpful?
Last updated
Was this helpful?
The main goal of abstraction is to handle complexities by hiding/encapsulating unnecessary details from other users. Abstraction is implemented in most languages by defining a class that has methods, properties & constructors.
Abstract will allow you to define two contexts of operation:
Classes
Functions
An abstract class allows you to make a template or blueprint for a class that will be eventually inherited from, so the inheriting class doesn't have to implement all of the methods. Therefore, abstract classes cannot be instantiated but only extended.
We would suggest that if you define abstract classes that you add the prefix Abstract
to the class name as best practice: AbstractAnimal, AbstractLogger, AbstractPerson
. This goes a long way to help with readability and standards.
As you can see from the example above, abstract functions can be defined ONLY in an abstract class. These functions are demarcated as abstract so inherited classes can implement them. You can have many abstract functions in your abstract class and you can also have many concrete functions as well:
Abstract classes can have both abstract and concrete methods defined within it. Abstract methods have no body, they are just declared, much like interfaces. Usually, you would do this to satisfy an interface declaration. In my years of experience, abstract classes usually go hand in hand with interfaces and usually implement .