Types
of Inheritance
Different forms of Inheritance:
1. Single inheritance: When a child class inherits from
only one parent class, it is called single inheritance. We saw an example
above.
2. Multiple inheritance: When a child class inherits
from multiple parent classes, it is called multiple inheritance.
Unlike Java and like C++, Python supports multiple inheritance. We specify all
parent classes as a comma-separated list in the bracket.
#
Python example to show the working of multiple #
inheritance classBase1(object):
def__init__(self):
self.str1
="Geek1" print("Base1")
classBase2(object):
def__init__(self):
self.str2
="Geek2" print("Base2")
classDerived(Base1,
Base2): def__init__(self):
#
Calling constructors of Base1 #
and Base2 classes Base1.__init__(self)
Base2.__init__(self)
print("Derived")
defprintStrs(self):
print(self.str1,
self.str2) ob
=Derived() ob.printStrs()
|
Output:
Base1
Base2
Derived
('Geek1',
'Geek2')
Output :
Base1
Base2
Derived
Geek1 True E101
3.
Multilevel inheritance: When we have a child and
grandchild relationship.
# A
Python program to demonstrate inheritance # Base
or Super class. Note object in bracket. #
(Generally, object is made ancestor of all classes) # In
Python 3.x "class Person" is #
equivalent to "class Person(object)" classBase(object):
#
Constructor def__init__(self,
name): self.name
=name #
To get name defgetName(self):
returnself.name
#
Inherited or Sub class (Note Person in bracket) classChild(Base):
#
Constructor def__init__(self,
name, age): Base.__init__(self,
name) self.age
=age #
To get name defgetAge(self):
returnself.age #
Inherited or Sub class (Note Person in bracket) classGrandChild(Child):
#
Constructor def__init__(self,
name, age, address): Child.__init__(self,
name, age) self.address
=address #
To get address defgetAddress(self):
returnself.address
#
Driver code g
=GrandChild("Geek1", 23, "Noida") print(g.getName(),
g.getAge(), g.getAddress()) |
Output:
Geek1 23 Noida
4. Hierarchical inheritance More than one derived
classes are created from a single base.
5. Hybrid inheritance: This form combines more than
one form of inheritance. Basically, it is a blend of more than one type of
inheritance.
Private members of parent class
We don’t always want the instance variables of the parent class to be inherited
by the child class i.e. we can make some of the instance variables of the
parent class private, which won’t be available to the child class.
We can make an instance variable by adding double underscores before its name.
For example,
#
Python program to demonstrate private members # of
the parent class classC(object):
def__init__(self):
self.c
=21 #
d is private instance variable self.__d
=42 classD(C):
def__init__(self):
self.e
=84 C.__init__(self)
object1
=D() #
produces an error as d is private instance variable print(object1.d)
|
Output :
File
"/home/993bb61c3e76cda5bb67bd9ea05956a1.py", line 16, in
print (D.d)
AttributeError:
type object 'D' has no attribute 'd'
Since ‘d’ is made private by those underscores, it
is not available to the child class ‘D’ and hence the error.
Comments
Post a Comment