- The same method or operreduce complexity.
- Improve security enforce consistent structure
- Separate interface from implementation
- Support large and scalable applicationsation to behave differently based on the object or context.
Python implements abstraction using:
- Abstract Classes
- Abstract Methods
- abc module
An abstract class is a class that:
-
cannot be instantiated
-
contains one or more abstract methods
-
acts as a blueprint for child classes
An abstract method is a method that:
-
has no implementation in the parent class
-
must be implemented in the child class
from abc import ABC, abstractmethod from abc import ABC, abstractmethod class Shape(ABC): @abstractmethod def draw(self): pass class Circle(Shape): def draw(self): print("Drawing Circle") class Square(Shape): def draw(self): print("Drawing Square") c = Circle() s = Square() c.draw() s.draw() #output Drawing Circle Drawing Square