Python Classroom Series – 03/Sept/2021

Adding a Method in the Child Class

  • Child class can add methods that are not present in parent class
  • Refer Here forthe examples done in the class
  • For properties
class Student:
    def __init__(self,name) -> None:
        self.__name = name

    @property
    def name(self):
        return self.__name

    @name.setter
    def name(self, value):
        self.__name = value



class Circle():
    def __init__(self, radius) -> None:
        self.__radius = radius

    @property
    def diameter(self):
        return 2 * self.__radius

    @property
    def area(self):
        import math
        return math.pi * self.__radius ** 2
        

s1 = Student('Ram')
print(s1.name)
s1.name = "Shyam"
print(s1.name)

c1 = Circle(10)
print(c1.diameter)
print(c1.area)


  • Name Mangling for privacy:
    • If you want hide your attribute begin the member name with two underscores Preview

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin