Python Abstraction
Abstraction is one of the four fundamental concepts in object-oriented programming (OOP). It refers to the process of hiding the internal details and showing only the essential features of an object. Abstraction helps in reducing complexity by focusing on what an object does instead of how it does it.
In Python, abstraction can be achieved through abstract classes and interfaces using the abc (Abstract Base Classes) module. Abstract classes are classes that contain one or more abstract methods, which are methods that are declared but have no implementation. These methods must be implemented in any subclass of the abstract class.
When Should You Use Abstract Classes?
● You have common functionality that you want to enforce across multiple derived classes.
● You want to prevent direct instantiation of a base class because it’s too general or incomplete.
● You want to enforce that certain methods must be implemented by every subclass (e.g., every animal must
have its own sound).
Example:
Scenario: Create a program to simulate different pets. Each pet can make its own sound and all pets can sleep. You will use an abstract class for common behaviors and specific classes for each pet.
from abc import ABC, abstractmethod
# Abstract class
class Animal(ABC):
# Abstract method for making sound
@abstractmethod
def make_sound(self):
pass
# subclass for Dog
class Dog(Animal):
def make_sound(self):
print(“The dog says: Woof Woof”)
# subclass for Cat
class Cat(Animal):
def make_sound(self):
print(“The cat says: Meow Meow”)
my_dog = Dog() # Create an instance of Dog
my_cat = Cat() # Create an instance of Cat
my_dog.make_sound() # Dog makes sound
my_cat.make_sound() # Cat makes sound
”’
Output:
The dog says: Woof Woof
The cat says: Meow Meow
”’
Course Video
Course Video English:
Task:
1. Basic Abstract Class and Method Implementation:
Create an abstract class Vehicle with an abstract method Start. Derive two classes Car and Bike from Vehicle. Implement the Start method in both derived classes to print “Car starting” and “Bike starting” respectively. In the Main method, create instances of Car and Bike and call the Start method on each.
2. Abstract Class with Properties:
Create an abstract class Person with an abstract property Name and an abstract method GetDetails. Derive two classes Student and Teacher from Person. Implement the Name property and GetDetails method in both derived classes. In the Main method, create instances of Student and Teacher, set their names, and call the GetDetails method to print their details.
3. Abstract Class with Constructor :
Create an abstract class Appliance with __init__() method that takes a string parameter brand and an abstract method ShowBrand. Derive two classes WashingMachine and Refrigerator from Appliance. Implement the ShowBrand method in both derived classes to print the brand. In the Main method, create instances of WashingMachine and Refrigerator and call the ShowBrand method.
4. Abstract Class with Multiple Methods:
Create an abstract class Shape with two abstract methods: CalculateArea and CalculatePerimeter. Derive two classes Circle and Rectangle from Shape. Implement the CalculateArea and CalculatePerimeter methods in both derived classes. In the Main method, create instances of Circle and Rectangle, set their dimensions, and call the methods to print the area and perimeter of each shape.
5. Abstract Class and Inheritance Hierarchy:
Create an abstract class Animal with an abstract method Speak. Derive a class Mammal from Animal and override the Speak method. Further, derive two classes Dog and Cat from Mammal and provide their specific implementations of the Speak method. In the Main method, create instances of Dog and Cat and call their Speak methods.