Inheritance
Inheritance
org/types-of-inheritance-python/
Class
A Class is a user-defined blueprint or prototype from which objects are created. It represents
the set of properties or methods that are common to all objects of one type. Using classes, you
can create multiple objects with the same behavior instead of writing their code multiple times.
This includes classes for objects occurring more than once in your code.
class Parrot:
# class attribute
name = ""
age = 0
Object
An object is any entity that has attributes and behaviors. For example, a parrot is an object. It
has
Example:
class Parrot:
# class attribute
name = ""
age = 0
parrot1 = Parrot()
parrot1.name = "Blu"
parrot1.age = 10
parrot2 = Parrot()
parrot2.name = "Woo"
parrot2.age = 15
# access attributes
Output
Inheritance is a way of creating a new class for using details of an existing class without
modifying it.
The newly formed class is a derived class (or child class). Similarly, the existing class is a
base class (or parent class).
Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and
there is already a class that includes some of the code that we want, we can derive our new
class from the existing class. By doing this, we are reusing the fields and methods of the existing
class.
The newly formed class is a derived class (or child class). Similarly, the existing class is a
base class (or parent class).
Single Inheritance:
Single inheritance enables a derived class to inherit properties from a single parent class, thus
enabling code reusability and the addition of new features to existing code.
Example:
# single inheritance
# Base class
class Parent:
def func1(self):
# Derived class
class Child(Parent):
def func2(self):
object = Child()
object.func1()
object.func2()
Output:
Multiple Inheritance:
When a class can be derived from more than one base class this type of inheritance is called
multiple inheritances. In multiple inheritances, all the features of the base classes are inherited
into the derived class.
class Mother:
mothername = ""
def mother(self):
print(self.mothername)
# Base class2
class Father:
fathername = ""
def father(self):
print(self.fathername)
# Derived class
def parents(self):
# Driver's code
s1 = Son()
s1.fathername = "RAM"
s1.mothername = "SITA"
s1.parents()
Output:
Father : RAM
Mother : SITA
Multilevel Inheritance :
In multilevel inheritance, features of the base class and the derived class are further inherited
into the new derived class. This is similar to a relationship representing a child and a
grandfather.
Example:
# Base class
class Grandfather:
self.grandfathername = grandfathername
# Intermediate class
class Father(Grandfather):
self.fathername = fathername
# invoking constructor of Grandfather class
Grandfather.__init__(self, grandfathername)
# Derived class
class Son(Father):
self.sonname = sonname
def print_name(self):
# Driver code
print(s1.grandfathername)
s1.print_name()
Output:
Lal mani
Hierarchical Inheritance:
When more than one derived class are created from a single base this type of inheritance is
called hierarchical inheritance. In this program, we have a parent (base) class and two child
(derived) classes.
class Parent:
def func1(self):
# Derived class1
class Child1(Parent):
def func2(self):
# Derivied class2
class Child2(Parent):
def func3(self):
object1 = Child1()
object2 = Child2()
object1.func1()
object1.func2()
object2.func1()
object2.func3()
Output:
Hybrid Inheritance:
Example:
# Python program to demonstrate
# hybrid inheritance
class School:
def func1(self):
class Student1(School):
def func2(self):
class Student2(School):
def func3(self):
def func4(self):
# Driver's code
object = Student3()
object.func1()
object.func2()
Output: