Abstraction Implementation
Click Link to view Abstraction.png
from abc import ABC, abstractmethod
class Shape(ABC):
"""Abstract base class for geometric shapes."""
@abstractmethod
def area(self):
"""Calculates and returns the area of the shape."""
pass
@abstractmethod
def perimeter(self):
"""Calculates and returns the perimeter of the shape."""
pass
class Rectangle(Shape):
"""Represents a rectangle."""
def __init__(self,length,breadth):
self.length = length
self.breadth = breadth
def area(self):
return self.length * self.breadth
def perimeter(self):
return 2 * (self.length + self.breadth)
class Circle(Shape):
"""Represents a circle."""
def __init__(self,radius):
self.radius = radius
def area(self):
return 3.14159 * (self.radius**2)
def perimeter(self):
return 2 * 3.14159 * self.radius
# Create instances of the shapes
rectangle = Rectangle(5, 3)
circle = Circle(4)
# Accessing methods through abstract base class
print("Rectangle area:", rectangle.area())
print("Rectangle perimeter:", rectangle.perimeter())
print("Circle area:", circle.area())
print("Circle perimeter:", circle.perimeter())