1.

How do you access parent members in the child class?

Answer»

Following are the ways using which you can access parent class members within a CHILD class:

  • By using Parent class name: You can use the name of the parent class to access the attributes as shown in the EXAMPLE below:
class Parent(object): # Constructor def __init__(self, name): self.name = name class Child(Parent): # Constructor def __init__(self, name, age): Parent.name = name self.age = age def display(self): PRINT(Parent.name, self.age) # Driver Codeobj = Child("Interviewbit", 6)obj.display()
  • By using super(): The parent class members can be accessed in child class using the super keyword.
class Parent(object): # Constructor def __init__(self, name): self.name = name class Child(Parent): # Constructor def __init__(self, name, age): '''  In Python 3.x, we can also use super().__init__(name) '''  super(Child, self).__init__(name) self.age = age def display(self): # Note that Parent.name cant be USED  # here since super() is used in the constructor print(self.name, self.age) # Driver Codeobj = Child("Interviewbit", 6)obj.display()


Discussion

No Comment Found