SlideShare a Scribd company logo
1 of 17
CHAPTER 4 POLYMORPHISM
By
Sirage Zeynu (M.Tech)
School of Computing
13-04-2021 POLYMORPHISM 2
 Polymorphism is the ability of an object to take on many
forms.
 In programming languages polymorphism is the capability of
an action or method to do different things based on the object
that it is acting upon.
 Polymorphism is one of the OOPs feature that allows us to
perform a single action in different ways.
 The three types of polymorphism are:
 ad-hoc (overloading and overriding), parametric (generics)
and dynamic method binding.
Introduction
13-04-2021 Polymorphism 3
Method Overloading in Java
This is an example of compile time (or static polymorphism)
13-04-2021 Polymorphism 4
13-04-2021 Polymorphism 5
Method Overriding in Java
This is an example of runtime time (or dynamic polymorphism)
 Method in subclass with same signature overrides method
from base class
 Overriding method is the one used for objects of the derived
class
 Overriding method must return same type of value
13-04-2021 Polymorphism 6
13-04-2021 Polymorphism 7
Polymor
phism
13-04-2021 Polymorphism 8
 Overloading happens at compile-time while Overriding happens at runtime: The
binding of overloaded method call to its definition has happens at compile-time
however binding of overridden method call to its definition happens at runtime.
 Static methods can be overloaded which means a class can have more than one static
method of same name. Static methods cannot be overridden, even if you declare a
same static method in child class it has nothing to do with the same method of
parent class.
 The most basic difference is that overloading is being done in the same class while
for overriding base and child classes are required. Overriding is all about giving a
specific implementation to the inherited method of parent class.
 Static binding is being used for overloaded methods and dynamic binding is being
used for overridden/overriding methods.
Overloading vs Overriding in Java
13-04-2021 Polymorphism 9
 Performance: Overloading gives better performance compared to
overriding. The reason is that the binding of overridden methods is
being done at runtime.
 private and final methods can be overloaded but they cannot be
overridden.
 Return type of method does not matter in case of method
overloading, it can be same or different. However in case of
method overriding the overriding method can have more specific
return type (refer this).
 Argument list should be different while doing method overloading.
Argument list should be same in method Overriding.
Overloading vs Overriding in Java
13-04-2021 Polymorphism 10
 Association of method call to the method body is known
as binding.
 There are two types of binding:
 Static Binding that happens at compile time
 Dynamic Binding that happens at runtime. Before
I explain static and dynamic binding in java, lets see few
terms that will help you understand this concept better.
Static and dynamic binding in java
13-04-2021 Polymorphism 11
class Human{
……
}
class Boy extends Human{
public static void main( String args[]) {
/*This statement simply creates an object of class
*Boy and assigns a reference of Boy to it*/
Boy obj1 = new Boy();
/* Since Boy extends Human class. The object creation
* can be done in this way. Parent class reference
* can have child class reference assigned to it
*/
Human obj2 = new Boy();
}
}
What is reference and object?
13-04-2021 Polymorphism 12
 The binding which can be resolved at compile time by compiler is
known as static or early binding.
 The binding of static, private and final methods is compile-time.
 Why?
 The reason is that the these method cannot be overridden and the
type of the class is determined at the compile time.
Static Binding or Early Binding
13-04-2021 Polymorphism 13
class Human{
public static void walk()
{
System.out.println("Human walks");
}
}
class Boy extends Human{
public static void walk(){
System.out.println("Boywalks");
}
public static void main( String args[]) {
/* Reference is of Human type and object is
Boy type
*/
Human obj = new Boy();
/* Reference is of HUman type and
object is of Human type.
*/
Human obj2 = new Human();
obj.walk();
obj2.walk();
}}
Static binding example
Output:
Human walks
Human walks
13-04-2021 Polymorphism 14
 When compiler is not able to resolve the call/binding at
compile time, such binding is known as Dynamic or late
Binding.
 Method Overriding is a perfect example of dynamic binding
as in overriding both parent and child classes have same
method and in this case the type of the object determines
which method is to be executed.
 The type of object is determined at the run time so this is
known as dynamic binding.
Dynamic Binding or Late Binding
13-04-2021 Polymorphism 15
Dynamic binding example
output
Boy walks
Human walks
13-04-2021 Polymorphism 16
 Lets discuss the difference between static and dynamic binding in
Java.
 Static binding happens at compile-time while dynamic binding
happens at runtime.
 Binding of private, static and final methods always happen at
compile time since these methods cannot be overridden.
 When the method overriding is actually happening and the reference
of parent type is assigned to the object of child class type then such
binding is resolved during runtime.
 The binding of overloaded methods is static and the binding of
overridden methods is dynamic.
Static Binding vs Dynamic Binding
13-04-2021 Polymorphism 18
What is Abstract Class in Java with example?
Why we need an abstract class?
Abstract class declaration and rules
What is Interface in java with example programs
What is the use of interface in Java?
Multiple inheritance and interfaces
What are the different between Abstract and concrete class ?
what are the Difference Between Abstract Class and Interface in Java?
Home work

More Related Content

What's hot

Review Session - Part -2
Review Session - Part -2Review Session - Part -2
Review Session - Part -2Hitesh-Java
 
Advanced OOP - Laws, Principles, Idioms
Advanced OOP - Laws, Principles, IdiomsAdvanced OOP - Laws, Principles, Idioms
Advanced OOP - Laws, Principles, IdiomsClint Edmonson
 
Extreme Interview Questions
Extreme Interview QuestionsExtreme Interview Questions
Extreme Interview QuestionsEhtisham Ali
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentationtigerwarn
 
Object Oriented Concepts in Real Projects
Object Oriented Concepts in Real ProjectsObject Oriented Concepts in Real Projects
Object Oriented Concepts in Real ProjectsEPAM
 
principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class designNeetu Mishra
 
Bt8903, c# programming
Bt8903, c# programmingBt8903, c# programming
Bt8903, c# programmingsmumbahelp
 
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Simplilearn
 

What's hot (12)

Review Session - Part -2
Review Session - Part -2Review Session - Part -2
Review Session - Part -2
 
OOP in Java
OOP in JavaOOP in Java
OOP in Java
 
Advanced OOP - Laws, Principles, Idioms
Advanced OOP - Laws, Principles, IdiomsAdvanced OOP - Laws, Principles, Idioms
Advanced OOP - Laws, Principles, Idioms
 
Extreme Interview Questions
Extreme Interview QuestionsExtreme Interview Questions
Extreme Interview Questions
 
Abstract Class Presentation
Abstract Class PresentationAbstract Class Presentation
Abstract Class Presentation
 
Object Oriented Concepts in Real Projects
Object Oriented Concepts in Real ProjectsObject Oriented Concepts in Real Projects
Object Oriented Concepts in Real Projects
 
Csci360 20 (1)
Csci360 20 (1)Csci360 20 (1)
Csci360 20 (1)
 
Csci360 20
Csci360 20Csci360 20
Csci360 20
 
Java interview question
Java interview questionJava interview question
Java interview question
 
principles of object oriented class design
principles of object oriented class designprinciples of object oriented class design
principles of object oriented class design
 
Bt8903, c# programming
Bt8903, c# programmingBt8903, c# programming
Bt8903, c# programming
 
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
Abstract Class In Java | Java Abstract Class Tutorial | Java Tutorial For Beg...
 

Similar to Chapter 4

Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+javaYe Win
 
Introducing java oop concepts
Introducing java oop conceptsIntroducing java oop concepts
Introducing java oop conceptsIvelin Yanev
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questionsnishajj
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In JavaSpotle.ai
 
polymorphism OOP.pptx
polymorphism OOP.pptxpolymorphism OOP.pptx
polymorphism OOP.pptxAssadLeo1
 
Learn java lessons_online
Learn java lessons_onlineLearn java lessons_online
Learn java lessons_onlinenishajj
 
P.7 media 2 polymorphism
P.7 media 2 polymorphismP.7 media 2 polymorphism
P.7 media 2 polymorphismahmadmuzaqqi
 
Learn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator OverloadingLearn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator OverloadingEng Teong Cheah
 
Unit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfUnit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfArpana Awasthi
 
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdfJAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdfnofakeNews
 
polymorphism-17013114666666666653806.pdf
polymorphism-17013114666666666653806.pdfpolymorphism-17013114666666666653806.pdf
polymorphism-17013114666666666653806.pdfkashafishfaq21
 

Similar to Chapter 4 (20)

Object+oriented+programming+in+java
Object+oriented+programming+in+javaObject+oriented+programming+in+java
Object+oriented+programming+in+java
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
Introducing java oop concepts
Introducing java oop conceptsIntroducing java oop concepts
Introducing java oop concepts
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 
polymorphism OOP.pptx
polymorphism OOP.pptxpolymorphism OOP.pptx
polymorphism OOP.pptx
 
Learn java lessons_online
Learn java lessons_onlineLearn java lessons_online
Learn java lessons_online
 
P.7 media 2 polymorphism
P.7 media 2 polymorphismP.7 media 2 polymorphism
P.7 media 2 polymorphism
 
Learn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator OverloadingLearn C# Programming Polymorphism & Operator Overloading
Learn C# Programming Polymorphism & Operator Overloading
 
Unit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfUnit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdf
 
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdfJAVA VIVA QUESTIONS_CODERS LODGE.pdf
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
 
polymorphism-17013114666666666653806.pdf
polymorphism-17013114666666666653806.pdfpolymorphism-17013114666666666653806.pdf
polymorphism-17013114666666666653806.pdf
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism in oop
Polymorphism in oopPolymorphism in oop
Polymorphism in oop
 

More from siragezeynu

More from siragezeynu (12)

2 programming with c# i
2 programming with c# i2 programming with c# i
2 programming with c# i
 
6 database
6 database 6 database
6 database
 
Recovered file 1
Recovered file 1Recovered file 1
Recovered file 1
 
Chapter one
Chapter oneChapter one
Chapter one
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Chapter 3i
Chapter 3iChapter 3i
Chapter 3i
 
Chapter 5
Chapter 5Chapter 5
Chapter 5
 
Lab4
Lab4Lab4
Lab4
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
6 chapter 6 record storage and primary file organization
6 chapter 6  record storage and primary file organization6 chapter 6  record storage and primary file organization
6 chapter 6 record storage and primary file organization
 

Recently uploaded

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Chapter 4

  • 1. CHAPTER 4 POLYMORPHISM By Sirage Zeynu (M.Tech) School of Computing
  • 2. 13-04-2021 POLYMORPHISM 2  Polymorphism is the ability of an object to take on many forms.  In programming languages polymorphism is the capability of an action or method to do different things based on the object that it is acting upon.  Polymorphism is one of the OOPs feature that allows us to perform a single action in different ways.  The three types of polymorphism are:  ad-hoc (overloading and overriding), parametric (generics) and dynamic method binding. Introduction
  • 3. 13-04-2021 Polymorphism 3 Method Overloading in Java This is an example of compile time (or static polymorphism)
  • 5. 13-04-2021 Polymorphism 5 Method Overriding in Java This is an example of runtime time (or dynamic polymorphism)  Method in subclass with same signature overrides method from base class  Overriding method is the one used for objects of the derived class  Overriding method must return same type of value
  • 8. 13-04-2021 Polymorphism 8  Overloading happens at compile-time while Overriding happens at runtime: The binding of overloaded method call to its definition has happens at compile-time however binding of overridden method call to its definition happens at runtime.  Static methods can be overloaded which means a class can have more than one static method of same name. Static methods cannot be overridden, even if you declare a same static method in child class it has nothing to do with the same method of parent class.  The most basic difference is that overloading is being done in the same class while for overriding base and child classes are required. Overriding is all about giving a specific implementation to the inherited method of parent class.  Static binding is being used for overloaded methods and dynamic binding is being used for overridden/overriding methods. Overloading vs Overriding in Java
  • 9. 13-04-2021 Polymorphism 9  Performance: Overloading gives better performance compared to overriding. The reason is that the binding of overridden methods is being done at runtime.  private and final methods can be overloaded but they cannot be overridden.  Return type of method does not matter in case of method overloading, it can be same or different. However in case of method overriding the overriding method can have more specific return type (refer this).  Argument list should be different while doing method overloading. Argument list should be same in method Overriding. Overloading vs Overriding in Java
  • 10. 13-04-2021 Polymorphism 10  Association of method call to the method body is known as binding.  There are two types of binding:  Static Binding that happens at compile time  Dynamic Binding that happens at runtime. Before I explain static and dynamic binding in java, lets see few terms that will help you understand this concept better. Static and dynamic binding in java
  • 11. 13-04-2021 Polymorphism 11 class Human{ …… } class Boy extends Human{ public static void main( String args[]) { /*This statement simply creates an object of class *Boy and assigns a reference of Boy to it*/ Boy obj1 = new Boy(); /* Since Boy extends Human class. The object creation * can be done in this way. Parent class reference * can have child class reference assigned to it */ Human obj2 = new Boy(); } } What is reference and object?
  • 12. 13-04-2021 Polymorphism 12  The binding which can be resolved at compile time by compiler is known as static or early binding.  The binding of static, private and final methods is compile-time.  Why?  The reason is that the these method cannot be overridden and the type of the class is determined at the compile time. Static Binding or Early Binding
  • 13. 13-04-2021 Polymorphism 13 class Human{ public static void walk() { System.out.println("Human walks"); } } class Boy extends Human{ public static void walk(){ System.out.println("Boywalks"); } public static void main( String args[]) { /* Reference is of Human type and object is Boy type */ Human obj = new Boy(); /* Reference is of HUman type and object is of Human type. */ Human obj2 = new Human(); obj.walk(); obj2.walk(); }} Static binding example Output: Human walks Human walks
  • 14. 13-04-2021 Polymorphism 14  When compiler is not able to resolve the call/binding at compile time, such binding is known as Dynamic or late Binding.  Method Overriding is a perfect example of dynamic binding as in overriding both parent and child classes have same method and in this case the type of the object determines which method is to be executed.  The type of object is determined at the run time so this is known as dynamic binding. Dynamic Binding or Late Binding
  • 15. 13-04-2021 Polymorphism 15 Dynamic binding example output Boy walks Human walks
  • 16. 13-04-2021 Polymorphism 16  Lets discuss the difference between static and dynamic binding in Java.  Static binding happens at compile-time while dynamic binding happens at runtime.  Binding of private, static and final methods always happen at compile time since these methods cannot be overridden.  When the method overriding is actually happening and the reference of parent type is assigned to the object of child class type then such binding is resolved during runtime.  The binding of overloaded methods is static and the binding of overridden methods is dynamic. Static Binding vs Dynamic Binding
  • 17. 13-04-2021 Polymorphism 18 What is Abstract Class in Java with example? Why we need an abstract class? Abstract class declaration and rules What is Interface in java with example programs What is the use of interface in Java? Multiple inheritance and interfaces What are the different between Abstract and concrete class ? what are the Difference Between Abstract Class and Interface in Java? Home work

Editor's Notes

  1. Overloaded methods are methods with the same name signature but either a different number of parameters or different types in the parameter list. For example 'spinning' a number may mean increase it, 'spinning' an image may mean rotate it by 90 degrees. By defining a method for handling each type of parameter you control the desired effect. Overridden methods are methods that are redefined within an inherited or subclass. They have the same signature and the subclass definition is used. Parametrics are generic typing procedures. Dynamic (or late) method binding is the ability of a program to resolve references to subclass methods at runtime. For example assume that three subclasses (Cow, Dog and Snake) have been created based on the Animal abstract class, each having their own speak() method. Although each method reference is to an Animal (but no animal objects exist), the program is will resolve the correct method reference at runtime.
  2. Here the method demo() is overloaded 3 times: first method has 1 int parameter, second method has 2 int parameters and third one is having double parameter. Which method is to be called is determined by the arguments we pass while calling methods. This happens at  compile time. so this type of polymorphism is known as compile time polymorphism.
  3. Here we have 4 versions of same method add. We are overloading the method add() here.
  4. Here we have two classes Human and Boy. Both the classes have same method walk() but the method is static, which means it cannot be overriden so even though I have used the object of Boy class while creating object obj, the parent class method is called by it. Because the reference is of Human type (parent class). So whenever a binding of static, private and final methods happen, type of the class is determined by the compiler at compile time and the binding happens then and there.
  5. this is the same example that we have seen above. The only difference here is that in this example, overriding is actually happening since these methods are not static, private and final. In case of overriding the call to the overriden method is determined at runtime by the type of object thus late binding happens. Lets see an example to understand this:
  6. Here we have two classes Human and Boy. Both the classes have same method walk() but the method is static, which means it cannot be overriden so even though I have used the object of Boy class while creating object obj, the parent class method is called by it. Because the reference is of Human type (parent class). So whenever a binding of static, private and final methods happen, type of the class is determined by the compiler at compile time and the binding happens then and there.