inheritance super class and sub class example

Super Class

class GeometricObject:
    def __init__(self, color = "green", filled = True):
        self.__color = color
        self.__filled = filled

    def getColor(self):
        return self.__color

    def setColor(self, color):
        self.__color = color

    def isFilled(self):
        return self.__filled

    def setFilled(self, filled):
        self.__filled = filled

    def __str__(self):
        return "color: " + self.__color + " and filled: " + str(self.__filled)


 

 

Sub Class

 

Circle

from GeometricObject import GeometricObject
import math # math.pi is used in the class

class Circle(GeometricObject):
    def __init__(self, radius):
        super().__init__()
        self.__radius = radius


    def getRadius(self):
        return self.__radius

    def setRadius(self, radius):
        self.__radius = radius

    def getArea(self):
        return self.__radius * self.__radius * math.pi

    def getDiameter(self):
        return 2 * self.__radius

    def getPerimeter(self):
        return 2 * self.__radius * math.pi

    def printCircle(self):
        print(self.__str__()+" radius: " + str(self.__radius))

 

Rectangle

from GeometricObject import  GeometricObject

class Rectangle(GeometricObject):
    def __int__(self, width = 1, height = 1):
        super().__init__()
        self.__width = width
        self.__height = height
    
    def getWidth(self):
        return self.__width
    
    def setWidth(self, width):
        self.__width = width
    
    def getHeight(self):
        return self.__height
    
    def setHeight(self, height):
        self.__height =  height
        
    def getArea(self):
        return self.__width * self.__height
    
    def getPerimeter(self):
        return 2 * (self.__width + self.__height)