Abstraction with ABC

Use the abc module to define abstract base classes and abstract methods.

IntermediateTopic: Object-Oriented Programs
Back

Python Abstraction with ABC Program

This program helps you to learn the fundamental structure and syntax of Python programming.

Try This Code
# Program to demonstrate abstraction with ABC

from abc import ABC, abstractmethod


class Shape(ABC):
    @abstractmethod
    def area(self):
        pass


class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * self.radius * self.radius


c = Circle(5)
print("Area:", c.area())
Output
Area: 78.5

Understanding Abstraction with ABC

Shape defines an abstract method 'area'; Circle provides a concrete implementation, enforcing a common interface.

Note: To write and run Python programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Python Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Python programs.

Table of Contents