i

Python Programming

Python Inheritance

Instead of starting from the scratch, you can create class by deriving it from preexisting class by listing parent class in parentheses after new class name.

The child class inherits attributes of its parent class, and you can use those attributes if they were defined in child class.

Syntax:

The derived classes are declared much like their parent.

class SubClassName (ParentClasss1[, ParentClass2, ...]):

   'Optional class documentation of string'

   class_suitee

Example

Live Demo

Output:

Calling the child constructor

Calling the child method

Calling the parent method

Parent the attribute : 200

Similar way, you can drive class from multiple parent classes as follows:

class A:        # define the your class A

class B:         # define the your class B

class C(A, B):   # subclass of the A and B