Posts

Showing posts with the label Python Object Oriented Programming - Abstract Classes

Python Object Oriented Programming - Abstract Classes

Image
An abstract class in Python is a class that provides an interface to its child classes by defining certain methods that its child class must implement. This class cannot be used to create objects. The abc (Abstract Base Classes) module is used to create abstract classes in Python. from abc import ABC, abstractmethod class AbstractClass(ABC):     @abstractmethod     def my_abstract_method(self):         pass Abstract methods An abstract method is a method that is declared in an abstract class but has no implementation. These methods must be implemented in any non-abstract class that inherits from the abstract class. If this is not the case, Python will throw an error when instantiating the class. An example of using abstract classes  Let's look at an example of an abstract class Person that has an abstract method sleep(). We will also create two child classes: Student and Employee , which implement the abstract method:sleep() from abc import ABC, abstractmethod class Person(A