Single Inheritance in Python

Previous
Next

Single Inheritance:

  • In parent-child relation, we can access the functionality of Parent through child.
  • We cannot access Child functionality using Parent.
  • Accessing static members in Parent and Child as follows:
class Parent:
    def m1():
        print("Parent's m1")
        return

class Child(Parent):
    def m2():
        print("Child's m2")
        return

class Inheritance:
    def main():
        Child.m1()
        Child.m2()

        Parent.m1()
        Parent.m2() # error :
        return

Inheritance.main()

Accessing dynamic functionality in Parent-Child relation:

class Parent:
    def m1(self):
        print("Parent's m1")
        return

class Child(Parent):
    def m2(self):
        print("Child's m2")
        return

class Inheritance:
    def main():
        c = Child()
        c.m1()
        c.m2()

        p = Parent()
        p.m1()
        p.m2() # error
        return

Inheritance.main()
Previous
Next

Add Comment

Courses Enquiry Form