Doubt related Inheritance

Can we use the getter and setter method in single - level - Interitance for example :-

I have a parent class in which I have put the getter method and I have another class that is child class and there has single - level - Interitance relation to both class and I’m putting the setter method inside the child class.so my question is if I give the value from child class with object reference variable like x.set_value(“Hello”) so can I see the value which is Hello by using getter method which is present in parent class.

1 Like

Hiii Ansh Srivastav,
It was a nice question please give us some time we will be back to you soon.
Regards,
Divya TA_DS

Hiii Ansh Srivastav,
You can definitely write the setter method in child class and getter in parent class, and when you set any value using child class method it will get updated in the parent class as well, which you can get from the getter method you have defined in the parent class.

Here is an example -
class parent:
def init(self):
self.age=0

def getter(self):
    return self.age

class child(parent):
def init(self):
pass

def setter(Self):
    parent.age = int(input("Enter the age"))

obj = child()
obj.setter()
print(“Age is:”, obj.getter())

2 Likes

Thank you, mam that was nice explanation. :slight_smile:

2 Likes