Howdy friends, After class and objects tutorial we are moving forward with Inheritance in Python and also Python Polymorphism. Let’s start:
What is Inheritance in Python?
Inheritance is an important point of the object-oriented concept in every programming languages.
Inheritance describes is a kind of relationship between two or more classes, abstracting common details into superclassand stored specific ones in the subclass.
- By using inheritance, We can create a class by deriving it from a preexisting class by listing the parent class in parentheses after the new class name, instead of starting from scratch.
- To create a child class, specify the parent class name inside the pair of parenthesis, followed by its name.
class Child(Parent): pass
- In Python, every class uses inheritance and is inherited from
- Hence, the below two definitions of
class MySubClass: pass
class MySubClass(object): pass
objectis known as a parent or superclass.
MySubClassis known as child or subclass or derived class .
Inheritance in Python Example:
class Person: def __init__(self, fname, lname): self.fname = fname self.lname = lname class Employee(Person): all_employees =  def __init__(self, fname, lname, empid): Person.__init__(self, fname, lname) self.empid = empid Employee.all_employees.append(self) p1 = Person('George', 'smith') e1 = Employee('Jack', 'simmons', 456342) e2 = Employee('John', 'williams', 123656) print(p1.fname, '-', p1.lname) print(e1.fname, '-', e1.empid) print(e2.fname, '-', e2.empid)
Employeeclass is derived from
Output of Inheritance in Python:
George - smith Jack - 456342 John - 123656
- In the above example,
__init __method of the parent class
Personto create its object.
We can always override parent class methods of Inheritance in Python. One reason for overriding parent’s methods is because we want some special or different functionality in the subclass.
Overriding Methods of Inheritance in Python Example:
class Parent: def myMethod(self): print ('Calling the parent method') class Child(Parent): def myMethod(self): print ('Calling the child method') a = Child() a.myMethod()
Extending Built-in Types
Inheritancefeature can be also used to extend the built-in classes like
- The following example extends
EmployeesList, which can identify employees, having a given search word in their first name.
class EmployeesList(list): def search(self, name): matching_employees =  for employee in Employee.all_employees: if name in employee.fname: matching_employees.append(employee.fname) return matching_employees
Polymorphism in Python 3
Polymorphism allows a subclass to override or change a specific behavior, exhibited by the parent class. Or we can say that the same function name (but different signatures) being uses for different types of code.
In the below example, you will find
Employeeclass with two methods
- Definition of
ContractEmployeeclass derived from
Employee. It overrides functionality of
getBonusmethods found in it’s parent class
class Employee(Person): all_employees = EmployeesList () def __init__(self, fname, lname, empid): Person.__init__(self, fname, lname) self.empid = empid Employee.all_employees.append(self) def getSalary(self): return 'You get Monthly salary.' def getBonus(self): return 'You are eligible for Bonus.'
class ContractEmployee(Employee): def getSalary(self): return 'You will not get Salary from Organization.' def getBonus(self): return 'You are not eligible for Bonus.' e1 = Employee('Jack', 'simmons', 456342) e2 = ContractEmployee('John', 'williams', 123656) print(e1.getBonus()) print(e2.getBonus())
You are eligible for Bonus. You are not eligible for Bonus.
class India(): def capital(self): print("New Delhi is the capital of India.") def language(self): print("Hindi is the main spoken language of India.") class USA(): def capital(self): print("Washington, D.C. is the capital of USA.") def language(self): print("English is the main language of USA.") object_ind = India() object_usa = USA() for country in (object_ind, object_usa): country.capital() country.language() country.type()
You can clearly see that in the above example how we use the same function name being uses for different types of code.
What will be the output of the following code?
def f(self): print(float()) print(hex(-255))
def f(self): print(float()) print(hex(-42))
x = B()
What will be the output of the following code?
a = 1
def f1(self): a = 2 class1.a += 1 print(class1.a, end=' ') print(a, end=' ')
2 2 3 2
Must Read the pervious articles:
In the above article, we have discussed Inheritance in Python and Polymorphism in Python3 with suitable examples. In the coming tutorial, we will discuss more on our python tutorials.