We have discussed following topics on Object Oriented Programming in Python
In this article, Inheritance is introduced.
One of the major advantages of Object Oriented Programming is re-use. Inheritance is one of the mechanisms to achieve the same. In inheritance, a class (usually called superclass) is inherited by another class (usually called subclass). The subclass adds some attributes to superclass.
Below is a sample Python program to show how inheritance is implemented in Python.
class Person( object ):
def __init__( self , name):
self .name = name
def getName( self ):
return self .name
def isEmployee( self ):
return False
class Employee(Person):
def isEmployee( self ):
return True
emp = Person( "Geek1" )
print (emp.getName(), emp.isEmployee())
emp = Employee( "Geek2" )
print (emp.getName(), emp.isEmployee())
|
Output:
('Geek1', False)
('Geek2', True)
How to check if a class is subclass of another?
Python provides a function issubclass() that directly tells us if a class is subclass of another class.
class Base( object ):
pass
class Derived(Base):
pass
print ( issubclass (Derived, Base))
print ( issubclass (Base, Derived))
d = Derived()
b = Base()
print ( isinstance (b, Derived))
print ( isinstance (d, Base))
|
Output:
True
False
False
True
What is object class?
Like Java Object class, in Python (from version 3.x), object is root of all classes.
In Python 3.x, “class Test(object)” and “class Test” are same.
In Python 2.x, “class Test(object)” creates a class with object as parent (called new style class) and “class Test” creates old style class (without object parent). Refer this for more details.
Does Python support Multiple Inheritance?
Unlike Java and like C++, Python supports multiple inheritance. We specify all parent classes as comma separated list in bracket.
class Base1( object ):
def __init__( self ):
self .str1 = "Geek1"
print "Base1"
class Base2( object ):
def __init__( self ):
self .str2 = "Geek2"
print "Base2"
class Derived(Base1, Base2):
def __init__( self ):
Base1.__init__( self )
Base2.__init__( self )
print "Derived"
def printStrs( self ):
print ( self .str1, self .str2)
ob = Derived()
ob.printStrs()
|
Output:
Base1
Base2
Derived
('Geek1', 'Geek2')
How to access parent members in a subclass?
- Using Parent class name
class Base( object ):
def __init__( self , x):
self .x = x
class Derived(Base):
def __init__( self , x, y):
Base.x = x
self .y = y
def printXY( self ):
print (Base.x, self .y)
d = Derived( 10 , 20 )
d.printXY()
|
- Using super()
We can also access parent class members using super.
class Base( object ):
def __init__( self , x):
self .x = x
class Derived(Base):
def __init__( self , x, y):
super (Derived, self ).__init__(x)
self .y = y
def printXY( self ):
print ( self .x, self .y)
d = Derived( 10 , 20 )
d.printXY()
|
Note that the above two methods are not exactly the same. In the next article on inheritance, we will covering following topics.
1) How super works? How accessing a member through super and parent class name are different?
2) How Diamond problem is handled in Python?
Exercise:
Predict the output of following Python programs
-
class X( object ):
def __init__( self , a):
self .num = a
def doubleup( self ):
self .num * = 2
class Y(X):
def __init__( self , a):
X.__init__( self , a)
def tripleup( self ):
self .num * = 3
obj = Y( 4 )
print (obj.num)
obj.doubleup()
print (obj.num)
obj.tripleup()
print (obj.num)
|
-
class Person( object ):
def __init__( self , name):
self .name = name
def getName( self ):
return self .name
def isEmployee( self ):
return False
class Employee(Person):
def __init__( self , name, eid):
super (Employee, self ).__init__(name)
self .empID = eid
def isEmployee( self ):
return True
def getID( self ):
return self .empID
emp = Employee( "Geek1" , "E101" )
print (emp.getName(), emp.isEmployee(), emp.getID())
|
Output:
('Geek1', True, 'E101')
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
07 Jun, 2022
Like Article
Save Article