Abstract. What does that mean?
Jessica Alves
Posted on September 8, 2022
In general terms, the abstract word means:
“what is not concrete, abstraction’s result”
or “which has a high level of generalization”.
When it comes to programming we will also see the abstract word in some context. Here is a situation you might come across.
Abstract Class
An abstract class is basically a class that will never allow you to directly create instances of it.
So why is it supposed to exist?
Although it’s a class that can't be instantiated directly, it’s meant to be designed as a parent class in an inheritance hierarchy of classes. Abstract classes can implement all kinds of methods, including the concrete ones. But usually abstract classes also contain abstract methods.
Abstract Method
It’s a method that is not yet implemented by the abstract class, but it needs to be implemented by any class that inherits from it.
Doing analogies we can say it’s a kind of boilerplate or framework or interface used by every class that inherits from the abstract class.
This topic came up when I was studying Python. Even though I don't believe it might be the best programming language to give examples related to that topic (I'll talk about that later on), I'll leave that fact aside for a while and write a simple example below.
class AbstractAnimal:
def __init__(self, specie_name):
self.specie_name = specie_name
def birth(self):
print(f"I'm an animal and my specie is {self.specie_name}.")
self.make_sound()
def make_sound(self):
raise NotImplementedError("You need to implement a make_sound method.")
class Cat(AbstractAnimal):
def __init__(self):
super().__init__("cat")
def make_sound(self):
print("Meow")
class Dog(AbstractAnimal):
def __init__(self):
super().__init__("dog")
def make_sound(self):
print("Woof!")
kitten = Cat()
kitten.birth()
puppy = Dog()
puppy.birth()
Console output:
>>> I'm an animal and my specie is cat.
Meow
I'm an animal and my specie is dog.
Woof!
In the example above we created an abstract class called AbstractAnimal
and used it to pass as a parent class to our other classes named Cat
and Dog
.
Note that the abstract class has a birth
method which calls a second method named make_sound
. This last one is our abstract method. It's declared in our abstract class but it's not implemented. Which means it should be implemented by the classes inheriting from the AbstractAnimal
class otherwise the program crashes raising an error.
Once the make_sound
method should be differently implemented depending on the class that inherits it, we can see the Dog
and Cat
classes implementing different pieces of code for the same method and when the method is called by their instances, their respective make_sound
methods are called.
And why did I say that Python might not be the best programming language to give examples related to this article topic?
Well this is just a note to have in mind: in Python it’s not required that abstract methods from the abstract class must be implemented by its child classes. In other programming languages that is an enforced behaviour (a child class must implement the abstract methods inherited by an abstract class).
Also different from other programming languages in Python there is no abstract keyword to define if a class or method is abstract or not. By convention we can create an abstract class in Python and it will be considered abstract as we will treat it like one. Which means we will not directly create any instances for that class. We can use the “abstract” word when defining the class as we did in our example above(instead of defining the name class as Animal
, for example, we can use AbstractAnimal
).
As I said: it's just a note. It won't stop you from learning the concept but there are other programming languages such as Java that implements it as a part of the language instead of just using conventions.
Posted on September 8, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.