Skip to content
thesarfo

Concept

Abstraction in Python

Hiding implementation details behind an abstract base class and abstractmethod, illustrated with Animal, Dog, and Cat.

views 0

Hiding complex implementation details and showing only the essential features of an object. In Python, abstraction is achieved through encapsulation and providing interfaces.

from abc import ABC, abstractmethod
class Animal(ABC): # Abstract class
@abstractmethod
def make_sound(self):
pass
class Dog(Animal):
def make_sound(self):
return "Bark!"
class Cat(Animal):
def make_sound(self):
return "Meow!"
dog = Dog()
cat = Cat()
print(dog.make_sound()) # Output: Bark!
print(cat.make_sound()) # Output: Meow!