OOP Lab Exercise in Python | Abstract Class Problem Statement
Learn abstraction in Python with a practical example using abstract classes and methods. Understand how to design classes for shapes like Rectangle and Circle.
Design a Python program to demonstrate the concept of Abstraction using abstract classes and methods.
Create an abstract class named Shape using the ABC module. The class should contain:
- An abstract method named
area() - An abstract method named
perimeter()
Then create the following child classes:
-
Rectangle- Attributes:
length,width
- Attributes:
-
Circle- Attribute:
radius
- Attribute:
Each child class must implement the abstract methods area() and perimeter() according to its own formula.
Tasks
-
Import the required module for abstraction.
-
Create the abstract class
Shape. -
Implement abstract methods in the child classes.
-
Create objects of both
RectangleandCircle. -
Display:
- Shape type
- Area
- Perimeter
-
Demonstrate that an object of the abstract class cannot be created directly.
Sample Output
Shape: Rectangle
Area: 50
Perimeter: 30
Shape: Circle
Area: 78.5
Perimeter: 31.4Instructions
- Use proper class structure and indentation.
- Use constructors (
__init__) to initialize attributes. - Use meaningful variable names.
- Add comments where necessary.
Learning Objectives
After completing this practical exam, students will be able to:
- Understand the concept of abstraction in OOP
- Use abstract classes and abstract methods in Python
- Implement inheritance with abstraction
- Write reusable and organized code
Abstraction helps in hiding implementation details and showing only essential features to the user. (gyata.ai)