Accessing overridden methods of Parent

Previous
Next

Accessing overridden functionality of Parent class:

super():

  • It is pre-defined method.
  • It is used to access Parent class functionality(super) from Child(sub).
class Grand:
    def fun(self):
        print("Grand")
        return

class Parent(Grand):
    def fun(self):
        super().fun()
        print("Parent")
        return

class Child(Parent):
    def fun(self):
        super().fun()
        print("Child")
        return
    
class Inheritance:
    def main():
        obj = Child()
        obj.fun()
        return

Inheritance.main()
  • We can access the functionality of all classes in the hierarchy from one place using super() method.
  • We need to specify the Class type along with object reference variable.
  • If we specify the Child type, it access Parent functionality.
class Grand:
    def fun(self):
        print("Grand")
        return

class Parent(Grand):
    def fun(self):
        print("Parent")
        return

class Child(Parent):
    def fun(self):
        print("Child")
        return
    
class Inheritance:
    def main():
        obj = Child()
        obj.fun()
        super(Child, obj).fun()
        super(Parent, obj).fun()
        return

Inheritance.main()
Previous
Next

Add Comment

Courses Enquiry Form