Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Python
## the first example
class quadriLateral:
    def __init__(self, a, b, c, d):
        self.side1=a
        self.side2=b
        self.side3=c
        self.side4=d

    def perimeter(self):
        p=self.side1 + self.side2 + self.side3 + self.side4
        print("perimeter=",p)

class rectangle(quadriLateral):
         def __init__(self, a, b):
            super().__init__(a, b, a, b)

class square(rectangle):
    def __init__(self, a):
        super().__init__(a, a)
    def area(self):
        a=pow(self.side1, 2)
        print('Area of Square: ', a)
##
###############
## the second example
class TwoDObject:
    def __init__(self,x:float=0,y:float=0):
        self.x = x
        self.y = y
    def move(self,xchange:float=0,ychange:float=0):
        self.x = self.x + xchange
        self.y = self.y + ychange
    def area(self):
        return 0

class Circle(TwoDObject):
    def __init__(self,radius:float=1 , x:float=0, y:float=0):
        self.radius = radius
        super().__init__(x,y) 
    def area(self):
        return (self.radius*self.radius * 3.14)    
    def size_change(self,percent:float):
        self.radius = self.radius * (percent/100)


What I have tried:

as I know the subclass can only add functionality and properties to the parent class, but can't remove them.
in the second example (TwoDObject class and sub.Cl. Circle) the Circle adds properties to the superclass (TWoD.) (it adds the property radius) and for me it is normal.
whereas in the first example and what I find cranky ( quadrilateral and rectangle )the rectangle removes properties from the superclass (c,d are removed and has only a and b)and the square removes 3 attributes.
MY question is how can the class remove attributes from the parent class and how to do it?
Posted
Updated 11-Jul-20 16:19pm
v6

You can ... ish.
You can't actually remove parent properties of methods, but it is possible to "hide" them: Hiding Inherited Properties and Tasks in Derived Classes[^]
 
Share this answer
 
I do not think that is possible in Python, see 9. Classes — Python 3.7.8 documentation[^]. And why would you want to?
 
Share this answer
 
Comments
Ahmad Qassym 11-Jul-20 16:27pm    
i saw this example in website:
https://www.tutorialsteacher.com/python/inheritance-in-python
Richard MacCutchan 12-Jul-20 4:41am    
I don't see anything relevant to your question there. It just explains standard inheritance rules.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900