Abstract Base Classes - Learn Object-Oriented Programming in Python
Abstract Base Classes - Learn Object-Oriented Programming in Python
(/learn)
https://www.educative.io/courses/learn-object-oriented-programming-in-python/BnJEN8ROmZx 1/5
02/02/2022, 21:46 Abstract Base Classes - Learn Object-Oriented Programming in Python
def area(self):
pass
def perimeter(self):
pass
class Square(Shape):
self.length = length
def area(self):
def perimeter(self):
return (4 * self.length)
shape = Shape()
square = Square(4)
In the example above, you can see that an instance of Shape can be
created even though an object from this class cannot stand on its own. The
Square class, which is the child class of Shape , actually implements the
methods area() and perimeter() of the Shape class. Shape class should
provide a blueprint for its child classes to implement methods in it. To
prevent the user from making a Shape class object, we use abstract base
classes.
Syntax#
To define an abstract base class, we use the abc module. The abstract
base class is inherited from the built-in ABC class. We have to use the
decorator @abstractmethod above the method that we want to declare as
an abstract method.
https://www.educative.io/courses/learn-object-oriented-programming-in-python/BnJEN8ROmZx 2/5
02/02/2022, 21:46 Abstract Base Classes - Learn Object-Oriented Programming in Python
from abc import ABC, abstractmethod
class ParentClass(ABC):
@abstractmethod
def method(self)
Example#
Below is a code implementation for abstract base classes:
main1.py
main1.py
main2.py
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Square(Shape):
self.length = length
shape = Shape()
# this code will not compile since Shape has abstract methods without
# method definitions in it
https://www.educative.io/courses/learn-object-oriented-programming-in-python/BnJEN8ROmZx 3/5
02/02/2022, 21:46 Abstract Base Classes - Learn Object-Oriented Programming in Python
As you can see above, the code does not compile since we have not defined
the abstract methods, area and perimeter , inside the parent class,
Shape , or the child class, Square . Let’s do it and see what happens:
main3.py
main3.py
main4.py
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Square(Shape):
self.length = length
def area(self):
def perimeter(self):
return (4 * self.length)
shape = Shape()
# this code will not compile since Shape has abstract methods without
# method definitions in it
Explanation#
When we define the methods, area and perimeter , in the child class
Square , an object of Shape cannot be instantiated, but an object of
https://www.educative.io/courses/learn-object-oriented-programming-in-python/BnJEN8ROmZx 4/5
02/02/2022, 21:46 Abstract Base Classes - Learn Object-Oriented Programming in Python
We allow the user to have a free hand over the definition of the
methods while also making sure that the methods are defined.
By using abstract base classes, we can control classes whose objects can or
cannot be created.
Back Next
Mark as Completed
Report an Issue
https://www.educative.io/courses/learn-object-oriented-programming-in-python/BnJEN8ROmZx 5/5