Explain Inheritance in Python with an example.


Inheritance allows One class to gain all the members(say attributes and methods) of another class. Inheritance provides code reusability, makes it easier to create and maintain an application. They are different types of inheritance supported by Python. They are: single, multi-level, hierarchical and multiple inheritance. The class from which we are inheriting is called super-class and the class that is inherited is called a derived / child class.
Single Inheritance – where a derived class acquires the members of a single super class.
multi-level inheritance – a derived class d1 in inherited from base class base1, and d2 is inherited from base2.
hierarchical inheritance – from one base class you can inherit any number of child classes
multiple inheritance – a derived class is inherited from more than one base class.

class ParentClass:
v1 = "from ParentClass - v1"
v2 = "from ParentClass - v2"class ChildClass(ParentClass):
passc = ChildClass() print(c.v1) print(c.v2)


© 2017