Write a python program to Implement Interface using class


This Python program defines two classes, Circle and Rectangle, that inherit from a common base class Shape. The program calculates the areas of circles and rectangles based on user input. Here's an explanation of the program:

  • Importing the math module: The program starts by importing the math module to access mathematical functions like pi and pow.
  • class Shape: This is the base class, which can be considered an interface in this context. It defines three methods: input, process, and output. These methods are placeholders for specific implementations in the derived classes but are not implemented here.
  • class Circle(Shape): This is the Circle class, which inherits from the Shape class. It represents a circle and is responsible for calculating its area.
    • __init__(self, rad=0.0): The constructor initializes the Circle object with a default radius of 0.0. It also initializes an area variable to 0.0.
    • setdata(self): This method prompts the user to enter the radius of the circle and sets it in the radius attribute.
    • circle_area(self): This method calculates the area of the circle using the formula π * radius^2 and stores the result in the area attribute.
    • getdata(self): This method prints the calculated area to the console.
  • class Rectangle(Shape): This is the Rectangle class, which inherits from the Shape class. It represents a rectangle and calculates its area.
    • __init__(self, len=0, bre=0): The constructor initializes the Rectangle object with default length and breadth values of 0. It also initializes an area attribute to 0.
    • setdata(self): This method prompts the user to enter the length and breadth of the rectangle and sets them in the respective attributes.
    • rect_area(self): This method calculates the area of the rectangle by multiplying the length and breadth and stores the result in the area attribute.
    • getdata(self): This method prints the calculated area to the console.
  • Creating instances of Circle and Rectangle:
    • c = Circle(): Creates an instance of the Circle class.
    • c.setdata(): Prompts the user to enter the radius and sets it.
    • c.circle_area(): Calculates the circle's area.
    • c.getdata(): Displays the area of the circle.
    • r = Rectangle(): Creates an instance of the Rectangle class.
    • r.setdata(): Prompts the user to enter the length and breadth and sets them.
    • r.rect_area(): Calculates the rectangle's area.
    • r.getdata(): Displays the area of the rectangle.

In summary, this program demonstrates how classes can inherit from a common base class (or interface), and each derived class provides its own implementations for specific methods. It calculates and displays the areas of circles and rectangles based on user input.


Source Code

import math
class Shape: #Interface
    def input(self):pass
    def process(self):pass
    def output(self):pass
 
class Circle(Shape):
    def __init__(self,rad=0.0):
        self.radius = rad
        self.area = 0.0
 
    def setdata(self):
        self.radius = float(input("Enter radius :"))
 
    def circle_area(self):
        self.area = math.pi*math.pow(self.radius,2)
 
    def getdata(self):
        print("Circle Area :",self.area)
 
class Rectangle(Shape):
    def __init__(self,len=0,bre=0):
        self.len = len
        self.bre = bre
        self.area = 0
 
    def setdata(self):
        self.len = int(input("Enter Length :"))
        self.bre = int(input("Enter Breadth :"))
 
    def rect_area(self):
        self.area = self.len*self.bre
 
    def getdata(self):
        print("Rectangle Area :",self.area)
 
c = Circle()
c.setdata()
c.circle_area()
c.getdata()
 
r = Rectangle()
r.setdata()
r.rect_area()
r.getdata()

Output

Enter radius :12
Circle Area : 452.3893421169302
Enter Length :23
Enter Breadth :31
Rectangle Area : 713

Example Programs