SlideShare a Scribd company logo
Inheritance
Polymorphism
Overloading
Overriding
What is Inheritance?
Inheritance is a powerful feature in object oriented
programming.
It is the capability of one class to derive or inherit the
properties from some another class.
Every Car is a vehicles. To show this relationship, we take an example.
Vehicles
Car
In this representation, we use an arrow towards the base class as a UML
(Unified Modeling Language) convention.
Vehicles can be called any of the following:
 Super Class
 Parent Class
 Base Class
And car is:
 Sub Class
 Child Class
 Derived Class
Inheritance Syntax
>>> class Person:
pass
>>> class Car(Vehicles):
pass
>>> issubclass(Car,Vehicles)
Here, class Car inherits from class Vehicles. We use the function
issubclass() to confirm that car is a subclass of person.
Types of Inheritance
Single
Inheritance
Multilevel
Inheritance
Multiple
Inheritance
Hierarchical
Inheritance
Hybrid
Inheritance
Single Inheritance
Child class inherits from only one parent class
Example:
class Animal:
def speak(self):
print("Animal Speaking")
#child class Lion inherits the base class Animal
class Lion(Animal):
def roar(self):
print(“Lion roaring")
d = Lion()
d.roar()
d.speak()
Lion roaring
Animal Speaking
Output:
Multiple inheritance
Inherit multiple base classes in the child class
Class 1 Class 2
Class N
Class 3
Example:
class Calculation1:
def Addition(self,x,y):
return x+y;
class Calculation2:
def Multiplication(self,x,y):
return a*b;
class Derived(Calculation1,Calculation2):
def Division(self,a,b):
return a/b;
d = Derived()
print(d.Addition(2,4))
print(d.Multiplication(2,4))
print(d.Division(2,4))
Output:
6
8
0.5
Multi-Level inheritance
Class 2
Class N
In Multi-level inheritance derived class inherits from another
derived class. There is no limit for level.
Class 1
Example:
class Animal:
def speak(self):
print("Animal Speaking")
#The child class Lion inherits the base class Animal
class Lion(Animal):
def roar(self):
print(“Lion roaring")
#The child class BabyLion inherits another child class Lion
class BabyLion(Lion):
def eat(self):
print("Eating meat...")
d = BabyLion()
d.roar()
d.speak()
d.eat()
Output:
Lion roaring
Animal Speaking
Eating meat...
Hierarchical inheritance
In hierarchical inheritance more than one derived classes are created from
a single base class.
Class 3
Class 2
Base Class
Class 1
Hybrid inheritance
Class 3
Class 4
Hybrid inheritance is a combination of multiple inheritance
and multilevel inheritance.
Class 1
Class 2
Polymorphism
 Polymorphism means many forms or multiple form.
In programming polymorphism means the same name
of function (but different parameters) that is used for
different types.
 Polymorphism simply means that we can call the
same method name with different parameters, and
depending on the parameters, it will do different
things.
Example:
len(“GKTCS")
len([5,2,8,4,45,75,3,92,33])
# returns 5 as result
# returns 9 as result
In this case the function len() taking string as input in the first case and is
taking list as input in the second case.
Overloading
Method
Overloading
Operator
Overloading
Overloading is the ability of a function or operator to behave differently dependin
g on the parameters passed on to the function or the operands on which the oper
ator operates.
Overloading
Method OR Function Overloading
Method overloading or function
overloading is a type of polymorphism in
which we can define a number of methods
with the same name but with a different
number of parameters as well as
parameters can be of different types.
Example:
# Takes two argument and print their Addition
def addition(a, b):
x = a + b
print(x)
# Takes three argument and print their Addition
def addition(a, b, c):
x = a + b + c
print(x)
# below line shows an error
#addition(7, 2)
# This line will call the second product method
addition(2, 5, 1)
Output:
 In the above code we have defined two addition method, but we can only
use the second addition method, as python does not supports method
overloading.
 We may define many method of same name and different argument but
we can only use the latest defined method. Calling the other method will
produce an error. Like here calling addition(7,2) will produce an error as
the latest defined addition method takes three arguments.
08
Operator Overloading
We can use ’+’ operator for adding numbers and at the
same time to concatenate strings. It is possible
because ’+’ operator is overloaded by both int class
and str class.
Example:
# Addition of two numbers
print(3 + 2)
# Concatenate two strings
print("GKTCS“ + “Innovations“)
# Product of two numbers
print(3 * 2)
# Repeat the String
print("GKTCS"*3)
Output:
5
GKTCS Innovations
6
GKTCSGKTCSGKTCS
Overriding
Override means having two methods with
the same name but doing different tasks.
It means that one of the methods
overrides the other.
The concept of Method overriding allows
us to change or override the Parent Class
function in the Child Class.
In Python, to override a method, you have to meet certain
conditions
 You can’t override a method within the same class. It means you
have to do it in the child class using the Inheritance concept.
 To override the Parent Class method, you have to create a method
in the Child class with the same name and the same number of
parameters.
Example:
# Python Method Overriding
class Employee:
def message(self):
print('This message is from Employee Class')
class Company(Employee):
def message(self):
print('This Company class is inherited from Employee’)
emp = Employee()
emp.message()
comp = Company()
comp.message()
Output:
'This message is from Employee Class'
'This Company class is inherited from Employee'

More Related Content

Similar to Inheritance_Polymorphism_Overloading_overriding.pptx

Application package
Application packageApplication package
Application package
JAYAARC
 

Similar to Inheritance_Polymorphism_Overloading_overriding.pptx (20)

Inheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxInheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptx
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
An Introduction to C# and .NET Framework (Basic)
An Introduction to C# and .NET Framework (Basic)An Introduction to C# and .NET Framework (Basic)
An Introduction to C# and .NET Framework (Basic)
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
JAVA-PPT'S.pdf
JAVA-PPT'S.pdfJAVA-PPT'S.pdf
JAVA-PPT'S.pdf
 
Application package
Application packageApplication package
Application package
 
Basics of oops concept
Basics of oops conceptBasics of oops concept
Basics of oops concept
 
polymorphism.pdf
polymorphism.pdfpolymorphism.pdf
polymorphism.pdf
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
 
Polymorphism & Templates
Polymorphism & TemplatesPolymorphism & Templates
Polymorphism & Templates
 
E3
E3E3
E3
 
Delphi qa
Delphi qaDelphi qa
Delphi qa
 
Learn java lessons_online
Learn java lessons_onlineLearn java lessons_online
Learn java lessons_online
 
Object Oriented Principles
Object Oriented PrinciplesObject Oriented Principles
Object Oriented Principles
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
C# interview
C# interviewC# interview
C# interview
 
Learn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator OverloadingLearn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator Overloading
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 
1669958779195.pdf
1669958779195.pdf1669958779195.pdf
1669958779195.pdf
 

More from MalligaarjunanN

More from MalligaarjunanN (20)

bro_nodejs-1 front end development .pdf
bro_nodejs-1 front end development  .pdfbro_nodejs-1 front end development  .pdf
bro_nodejs-1 front end development .pdf
 
Microprocessor and microcontroller record.pdf
Microprocessor and microcontroller record.pdfMicroprocessor and microcontroller record.pdf
Microprocessor and microcontroller record.pdf
 
8087 MICROPROCESSOR and diagram with definition.pdf
8087 MICROPROCESSOR and diagram with definition.pdf8087 MICROPROCESSOR and diagram with definition.pdf
8087 MICROPROCESSOR and diagram with definition.pdf
 
8089 microprocessor with diagram and analytical
8089 microprocessor with diagram and analytical8089 microprocessor with diagram and analytical
8089 microprocessor with diagram and analytical
 
English article power point presentation eng.pptx
English article power point presentation eng.pptxEnglish article power point presentation eng.pptx
English article power point presentation eng.pptx
 
Digital principle and computer design Presentation (1).pptx
Digital principle and computer design Presentation (1).pptxDigital principle and computer design Presentation (1).pptx
Digital principle and computer design Presentation (1).pptx
 
Technical English grammar and tenses.pptx
Technical English grammar and tenses.pptxTechnical English grammar and tenses.pptx
Technical English grammar and tenses.pptx
 
Polymorphism topic power point presentation li.pptx
Polymorphism topic power point presentation li.pptxPolymorphism topic power point presentation li.pptx
Polymorphism topic power point presentation li.pptx
 
Chemistry iconic bond topic chem ppt.pptx
Chemistry iconic bond topic chem ppt.pptxChemistry iconic bond topic chem ppt.pptx
Chemistry iconic bond topic chem ppt.pptx
 
C programming DOC-20230723-WA0001..pptx
C programming  DOC-20230723-WA0001..pptxC programming  DOC-20230723-WA0001..pptx
C programming DOC-20230723-WA0001..pptx
 
Chemistry fluorescent topic chemistry.pptx
Chemistry fluorescent topic  chemistry.pptxChemistry fluorescent topic  chemistry.pptx
Chemistry fluorescent topic chemistry.pptx
 
C programming power point presentation c ppt.pptx
C programming power point presentation c ppt.pptxC programming power point presentation c ppt.pptx
C programming power point presentation c ppt.pptx
 
Python programming file handling mhhk.pptx
Python programming file handling mhhk.pptxPython programming file handling mhhk.pptx
Python programming file handling mhhk.pptx
 
Computer organisation and architecture updated unit 2 COA ppt.pptx
Computer organisation and architecture updated unit 2 COA ppt.pptxComputer organisation and architecture updated unit 2 COA ppt.pptx
Computer organisation and architecture updated unit 2 COA ppt.pptx
 
Data structures trees and graphs - Heap Tree.pptx
Data structures trees and graphs - Heap Tree.pptxData structures trees and graphs - Heap Tree.pptx
Data structures trees and graphs - Heap Tree.pptx
 
Data structures trees and graphs - AVL tree.pptx
Data structures trees and graphs - AVL  tree.pptxData structures trees and graphs - AVL  tree.pptx
Data structures trees and graphs - AVL tree.pptx
 
Data structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptxData structures trees - B Tree & B+Tree.pptx
Data structures trees - B Tree & B+Tree.pptx
 
Computer organisation and architecture .
Computer organisation and architecture .Computer organisation and architecture .
Computer organisation and architecture .
 
Python programming variables and comment
Python programming variables and commentPython programming variables and comment
Python programming variables and comment
 
pythoncommentsandvariables-231016105804-9a780b91 (1).pptx
pythoncommentsandvariables-231016105804-9a780b91 (1).pptxpythoncommentsandvariables-231016105804-9a780b91 (1).pptx
pythoncommentsandvariables-231016105804-9a780b91 (1).pptx
 

Recently uploaded

grop material handling.pdf and resarch ethics tth
grop material handling.pdf and resarch ethics tthgrop material handling.pdf and resarch ethics tth
grop material handling.pdf and resarch ethics tth
AmanyaSylus
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
Kamal Acharya
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
AbrahamGadissa
 

Recently uploaded (20)

Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
The battle for RAG, explore the pros and cons of using KnowledgeGraphs and Ve...
 
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWINGBRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
BRAKING SYSTEM IN INDIAN RAILWAY AutoCAD DRAWING
 
An improvement in the safety of big data using blockchain technology
An improvement in the safety of big data using blockchain technologyAn improvement in the safety of big data using blockchain technology
An improvement in the safety of big data using blockchain technology
 
grop material handling.pdf and resarch ethics tth
grop material handling.pdf and resarch ethics tthgrop material handling.pdf and resarch ethics tth
grop material handling.pdf and resarch ethics tth
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and ClusteringKIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
KIT-601 Lecture Notes-UNIT-4.pdf Frequent Itemsets and Clustering
 
Supermarket billing system project report..pdf
Supermarket billing system project report..pdfSupermarket billing system project report..pdf
Supermarket billing system project report..pdf
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and VisualizationKIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
KIT-601 Lecture Notes-UNIT-5.pdf Frame Works and Visualization
 
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES  INTRODUCTION UNIT-IENERGY STORAGE DEVICES  INTRODUCTION UNIT-I
ENERGY STORAGE DEVICES INTRODUCTION UNIT-I
 
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
NO1 Pandit Black Magic Removal in Uk kala jadu Specialist kala jadu for Love ...
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 
Peek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdfPeek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdf
 
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamKIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Digital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdfDigital Signal Processing Lecture notes n.pdf
Digital Signal Processing Lecture notes n.pdf
 
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdfRESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
RESORT MANAGEMENT AND RESERVATION SYSTEM PROJECT REPORT.pdf
 
A case study of cinema management system project report..pdf
A case study of cinema management system project report..pdfA case study of cinema management system project report..pdf
A case study of cinema management system project report..pdf
 
Dairy management system project report..pdf
Dairy management system project report..pdfDairy management system project report..pdf
Dairy management system project report..pdf
 

Inheritance_Polymorphism_Overloading_overriding.pptx

  • 2. What is Inheritance? Inheritance is a powerful feature in object oriented programming. It is the capability of one class to derive or inherit the properties from some another class.
  • 3. Every Car is a vehicles. To show this relationship, we take an example. Vehicles Car In this representation, we use an arrow towards the base class as a UML (Unified Modeling Language) convention.
  • 4. Vehicles can be called any of the following:  Super Class  Parent Class  Base Class And car is:  Sub Class  Child Class  Derived Class
  • 5. Inheritance Syntax >>> class Person: pass >>> class Car(Vehicles): pass >>> issubclass(Car,Vehicles) Here, class Car inherits from class Vehicles. We use the function issubclass() to confirm that car is a subclass of person.
  • 7. Single Inheritance Child class inherits from only one parent class Example: class Animal: def speak(self): print("Animal Speaking") #child class Lion inherits the base class Animal class Lion(Animal): def roar(self): print(“Lion roaring") d = Lion() d.roar() d.speak()
  • 9. Multiple inheritance Inherit multiple base classes in the child class Class 1 Class 2 Class N Class 3
  • 10. Example: class Calculation1: def Addition(self,x,y): return x+y; class Calculation2: def Multiplication(self,x,y): return a*b; class Derived(Calculation1,Calculation2): def Division(self,a,b): return a/b; d = Derived() print(d.Addition(2,4)) print(d.Multiplication(2,4)) print(d.Division(2,4))
  • 12. Multi-Level inheritance Class 2 Class N In Multi-level inheritance derived class inherits from another derived class. There is no limit for level. Class 1
  • 13. Example: class Animal: def speak(self): print("Animal Speaking") #The child class Lion inherits the base class Animal class Lion(Animal): def roar(self): print(“Lion roaring") #The child class BabyLion inherits another child class Lion class BabyLion(Lion): def eat(self): print("Eating meat...") d = BabyLion() d.roar() d.speak() d.eat()
  • 15. Hierarchical inheritance In hierarchical inheritance more than one derived classes are created from a single base class. Class 3 Class 2 Base Class Class 1
  • 16. Hybrid inheritance Class 3 Class 4 Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance. Class 1 Class 2
  • 17. Polymorphism  Polymorphism means many forms or multiple form. In programming polymorphism means the same name of function (but different parameters) that is used for different types.  Polymorphism simply means that we can call the same method name with different parameters, and depending on the parameters, it will do different things.
  • 18. Example: len(“GKTCS") len([5,2,8,4,45,75,3,92,33]) # returns 5 as result # returns 9 as result In this case the function len() taking string as input in the first case and is taking list as input in the second case.
  • 19. Overloading Method Overloading Operator Overloading Overloading is the ability of a function or operator to behave differently dependin g on the parameters passed on to the function or the operands on which the oper ator operates. Overloading
  • 20. Method OR Function Overloading Method overloading or function overloading is a type of polymorphism in which we can define a number of methods with the same name but with a different number of parameters as well as parameters can be of different types.
  • 21. Example: # Takes two argument and print their Addition def addition(a, b): x = a + b print(x) # Takes three argument and print their Addition def addition(a, b, c): x = a + b + c print(x) # below line shows an error #addition(7, 2) # This line will call the second product method addition(2, 5, 1)
  • 22. Output:  In the above code we have defined two addition method, but we can only use the second addition method, as python does not supports method overloading.  We may define many method of same name and different argument but we can only use the latest defined method. Calling the other method will produce an error. Like here calling addition(7,2) will produce an error as the latest defined addition method takes three arguments. 08
  • 23. Operator Overloading We can use ’+’ operator for adding numbers and at the same time to concatenate strings. It is possible because ’+’ operator is overloaded by both int class and str class.
  • 24. Example: # Addition of two numbers print(3 + 2) # Concatenate two strings print("GKTCS“ + “Innovations“) # Product of two numbers print(3 * 2) # Repeat the String print("GKTCS"*3)
  • 26. Overriding Override means having two methods with the same name but doing different tasks. It means that one of the methods overrides the other. The concept of Method overriding allows us to change or override the Parent Class function in the Child Class.
  • 27. In Python, to override a method, you have to meet certain conditions  You can’t override a method within the same class. It means you have to do it in the child class using the Inheritance concept.  To override the Parent Class method, you have to create a method in the Child class with the same name and the same number of parameters.
  • 28. Example: # Python Method Overriding class Employee: def message(self): print('This message is from Employee Class') class Company(Employee): def message(self): print('This Company class is inherited from Employee’) emp = Employee() emp.message() comp = Company() comp.message()
  • 29. Output: 'This message is from Employee Class' 'This Company class is inherited from Employee'