SlideShare a Scribd company logo
Polymorphism in Java
Method Overloading and Method Overriding
Polymorphism in Java
Method Overloading and Method Overriding
Polymorphism come from the two Greek words ‘poly’ meaning many and
‘morphs” meaning forms. The ability to exist in different form is called
polymorphism. The same variable or method can perform different tasks;
the programmer has the advantage of writing flexible code.
Polymorphism in Java
Method Overloading and Method Overriding
Types of Polymorphism
1. Static polymorphism (compile time polymorphism)
2. Dynamic polymorphism (Run time polymorphism)
1. Static polymorphism (compile time polymorphism)
If a method call is resolved at compile time that it is called static binding or
earlier binding or static polymorphism or compile time polymorphism.
Example-
Method overloading and method overriding by using static method, private
method and final method are example for static polymorphism.
Polymorphism in Java
Method Overloading and Method Overriding
2. Dynamic Polymorphism (Run Time Polymorphism)
 
If a method call is resolved at the time of execution it is called dynamic
binding or late binding or run time polymorphism.
 
Example-
Method overloading and method overriding by using instance method are
example for dynamic binding.
Binding
Resolving a method call that is identifying a definition to be executed is called
binding.
In java method call resolve according to the following rules-
Polymorphism in Java
Method Overloading and Method Overriding
S.R. Compile Time Polymorphism Run Time Polymorphism
1. Static method call are static binded. Instance method call is dynamic binded.
2. Private non static method call are static
binded.
3. Constructor call is statically binded.
4. Final method call is statically binded.
5. Call to non static, non private method
is statically binded when calling using
super keyword.
In JVM assembly there are 4 instructions which are used by JRE for
invoking method-
Polymorphism in Java
Method Overloading and Method Overriding
Polymorphism in Java
Method Overloading and Method Overriding
S.R JVM assembly Instruction Purpose Type of binding
1. Invokestatic Is used to invoke
static method
Static
2. Invokespecial Is used to invoke-
1- constructor
2- private non static
methods
3- non private, non
static method using
super
4- final method
Static
3. Invokevirtual Is used to invoke
Instance method
( non private, non
static without super)
Dynamic
4. invokeinterface Is used to invoke
interface method
Dynamic
Note:-
To see assembly instruction in class file:-
Syntax:-
javap -c classFileName
Example:-
javap -c Calculate
 
Method overloading
Writing two or more method in the same class in such a way that each
method has same name but with different method signature ( by varying
there number of argument or varying the type of argument or varying the
order of the argument) is called method overloading.
Method overloading is one of the means of implementing polymorphism.
Example-
public classCalculate
{
void sum(int a, int b)
{
System.out.println("Sum of two number="+(a+b));
}
void sum(inta,int b, int c)
{
System.out.println("Sum of ThreeNumber="+(a+b+c));
}
public static void main(String args[])
{
Calculatex=new Calculate();
x.sum(2,3);
x.sum(2,3,4);
}
}
Output:
Sum of two number=5
Sum of ThreeNumber=9
Note- Herepolymorphism isimplemented in sameclass.
Method Overriding
Writing two or more method in super and sub class such that the method have
same name and same signature is called method overriding. Method overriding
is one of the means of implementing polymorphism.
Example-
Programmethodoverriding
classCalculate
{
void sum(int a, int b)
{
System.out.println("Sum of two number="+(a+b));
}
}
classUseextendsCalculate
{
void sum(int a, int b)
{
System.out.println("Sum of Two Number (overloaded method)="+(a+b));
}
public static void main(String args[])
{
Usex=new Use();
x.sum(2,3);
}
}
Output:
Sum of Two Number (overloaded method)=5
Note- Herepolymorphism isimplemented in super and sub class.
WWW.JAVATPORTAL.COM
India's No. one Tutorials and a Solution of all Technology
Address:-
Sector 58 Noida, Uttar Pradesh 201301, India.
Mobile: +91 9458548058, 8860449650
Email: sales.javatportal@gmail.com
Website: http://www.javatportal.com

More Related Content

What's hot

Polymorphism
PolymorphismPolymorphism
Polymorphism
Nochiketa Chakraborty
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
CharthaGaglani
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Ahmed Za'anin
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Raffaele Doti
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
Harshal Misalkar
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
Janu Jahnavi
 
Inheritance
InheritanceInheritance
Inheritance
Sapna Sharma
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
Githushan Gengaparam
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
kamal kotecha
 
OOP - Polymorphism
OOP - PolymorphismOOP - Polymorphism
OOP - Polymorphism
Mudasir Qazi
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
Lovely Professional University
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
OOP java
OOP javaOOP java
OOP java
xball977
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
ShahinAhmed49
 
Packages in java
Packages in javaPackages in java
Packages in java
Abhishek Khune
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
BHUVIJAYAVELU
 

What's hot (20)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 
Inheritance
InheritanceInheritance
Inheritance
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
OOP - Polymorphism
OOP - PolymorphismOOP - Polymorphism
OOP - Polymorphism
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Java threads
Java threadsJava threads
Java threads
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
OOP java
OOP javaOOP java
OOP java
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 

Viewers also liked

Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
M. Raihan
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Kumar Gaurav
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
Mahi Mca
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
Tareq Hasan
 
polymorphism
polymorphism polymorphism
polymorphism
Imtiaz Hussain
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Duane Wesley
 
Java API
Java APIJava API
Danelle Williams Resume_Healthcare Administration (2)
Danelle Williams Resume_Healthcare Administration (2)Danelle Williams Resume_Healthcare Administration (2)
Danelle Williams Resume_Healthcare Administration (2)
Danelle Williams
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
Method overriding
Method overridingMethod overriding
Method overriding
Azaz Maverick
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method Overloading
Michael Heron
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
Michael Heron
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
Abbas Ajmal
 
Java API for XML Web Services (JAX-WS)
Java API for XML Web Services (JAX-WS)Java API for XML Web Services (JAX-WS)
Java API for XML Web Services (JAX-WS)
Peter R. Egli
 
Mutual fund
Mutual fundMutual fund
Mutual fund
Mathew Richardson
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
Jussi Pohjolainen
 
Abstraction in java
Abstraction in javaAbstraction in java
Abstraction in java
sawarkar17
 

Viewers also liked (17)

Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
polymorphism
polymorphism polymorphism
polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java API
Java APIJava API
Java API
 
Danelle Williams Resume_Healthcare Administration (2)
Danelle Williams Resume_Healthcare Administration (2)Danelle Williams Resume_Healthcare Administration (2)
Danelle Williams Resume_Healthcare Administration (2)
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Method overriding
Method overridingMethod overriding
Method overriding
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method Overloading
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
 
Java API for XML Web Services (JAX-WS)
Java API for XML Web Services (JAX-WS)Java API for XML Web Services (JAX-WS)
Java API for XML Web Services (JAX-WS)
 
Mutual fund
Mutual fundMutual fund
Mutual fund
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Abstraction in java
Abstraction in javaAbstraction in java
Abstraction in java
 

Similar to Polymorphism in java, method overloading and method overriding

Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Lovely Professional University
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Ducat India
 
Polymorphism_main.pptx
Polymorphism_main.pptxPolymorphism_main.pptx
Polymorphism_main.pptx
MDBONIAMIN213154366
 
P.7 media 2 polymorphism
P.7 media 2 polymorphismP.7 media 2 polymorphism
P.7 media 2 polymorphism
ahmadmuzaqqi
 
Learn java lessons_online
Learn java lessons_onlineLearn java lessons_online
Learn java lessons_online
nishajj
 
Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)
Geekster
 
Oop lecture 06
Oop lecture 06Oop lecture 06
Oop lecture 06
University of Chitral
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questions
Vinay Kumar
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
talha ijaz
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.doc
Joyce Thomas
 
Unit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfUnit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdf
Arpana Awasthi
 
28c
28c28c
28csharp
28csharp28csharp
28csharp
Sireesh K
 
JAVA_POLYMORPHISM.pptx
JAVA_POLYMORPHISM.pptxJAVA_POLYMORPHISM.pptx
JAVA_POLYMORPHISM.pptx
Hitesh Kumar Nath
 
Www javatpoint-com-corejava-interview-questions-2
Www javatpoint-com-corejava-interview-questions-2Www javatpoint-com-corejava-interview-questions-2
Www javatpoint-com-corejava-interview-questions-2
ssuserd2d58b
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
KartikKapgate
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
siragezeynu
 
polymorphism method overloading and overriding .pptx
polymorphism method overloading  and overriding .pptxpolymorphism method overloading  and overriding .pptx
polymorphism method overloading and overriding .pptx
thamaraiselvangts441
 
Advance Java Polymorphism OOPS CON .pptx
Advance Java Polymorphism OOPS CON .pptxAdvance Java Polymorphism OOPS CON .pptx
Advance Java Polymorphism OOPS CON .pptx
jprince1621
 
Chapter4__Method_Lim Lyheng_RULE (2).pdf
Chapter4__Method_Lim Lyheng_RULE (2).pdfChapter4__Method_Lim Lyheng_RULE (2).pdf
Chapter4__Method_Lim Lyheng_RULE (2).pdf
MornThea
 

Similar to Polymorphism in java, method overloading and method overriding (20)

Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism_main.pptx
Polymorphism_main.pptxPolymorphism_main.pptx
Polymorphism_main.pptx
 
P.7 media 2 polymorphism
P.7 media 2 polymorphismP.7 media 2 polymorphism
P.7 media 2 polymorphism
 
Learn java lessons_online
Learn java lessons_onlineLearn java lessons_online
Learn java lessons_online
 
Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)Java Polymorphism: Types And Examples (Geekster)
Java Polymorphism: Types And Examples (Geekster)
 
Oop lecture 06
Oop lecture 06Oop lecture 06
Oop lecture 06
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questions
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.doc
 
Unit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfUnit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdf
 
28c
28c28c
28c
 
28csharp
28csharp28csharp
28csharp
 
JAVA_POLYMORPHISM.pptx
JAVA_POLYMORPHISM.pptxJAVA_POLYMORPHISM.pptx
JAVA_POLYMORPHISM.pptx
 
Www javatpoint-com-corejava-interview-questions-2
Www javatpoint-com-corejava-interview-questions-2Www javatpoint-com-corejava-interview-questions-2
Www javatpoint-com-corejava-interview-questions-2
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
polymorphism method overloading and overriding .pptx
polymorphism method overloading  and overriding .pptxpolymorphism method overloading  and overriding .pptx
polymorphism method overloading and overriding .pptx
 
Advance Java Polymorphism OOPS CON .pptx
Advance Java Polymorphism OOPS CON .pptxAdvance Java Polymorphism OOPS CON .pptx
Advance Java Polymorphism OOPS CON .pptx
 
Chapter4__Method_Lim Lyheng_RULE (2).pdf
Chapter4__Method_Lim Lyheng_RULE (2).pdfChapter4__Method_Lim Lyheng_RULE (2).pdf
Chapter4__Method_Lim Lyheng_RULE (2).pdf
 

Recently uploaded

বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
spdendr
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
Chevonnese Chevers Whyte, MBA, B.Sc.
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 
Solutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptxSolutons Maths Escape Room Spatial .pptx
Solutons Maths Escape Room Spatial .pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Constructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective CommunicationConstructing Your Course Container for Effective Communication
Constructing Your Course Container for Effective Communication
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 

Polymorphism in java, method overloading and method overriding

  • 1. Polymorphism in Java Method Overloading and Method Overriding Polymorphism in Java Method Overloading and Method Overriding Polymorphism come from the two Greek words ‘poly’ meaning many and ‘morphs” meaning forms. The ability to exist in different form is called polymorphism. The same variable or method can perform different tasks; the programmer has the advantage of writing flexible code.
  • 2. Polymorphism in Java Method Overloading and Method Overriding Types of Polymorphism 1. Static polymorphism (compile time polymorphism) 2. Dynamic polymorphism (Run time polymorphism) 1. Static polymorphism (compile time polymorphism) If a method call is resolved at compile time that it is called static binding or earlier binding or static polymorphism or compile time polymorphism. Example- Method overloading and method overriding by using static method, private method and final method are example for static polymorphism.
  • 3. Polymorphism in Java Method Overloading and Method Overriding 2. Dynamic Polymorphism (Run Time Polymorphism)   If a method call is resolved at the time of execution it is called dynamic binding or late binding or run time polymorphism.   Example- Method overloading and method overriding by using instance method are example for dynamic binding. Binding Resolving a method call that is identifying a definition to be executed is called binding. In java method call resolve according to the following rules-
  • 4. Polymorphism in Java Method Overloading and Method Overriding S.R. Compile Time Polymorphism Run Time Polymorphism 1. Static method call are static binded. Instance method call is dynamic binded. 2. Private non static method call are static binded. 3. Constructor call is statically binded. 4. Final method call is statically binded. 5. Call to non static, non private method is statically binded when calling using super keyword. In JVM assembly there are 4 instructions which are used by JRE for invoking method-
  • 5. Polymorphism in Java Method Overloading and Method Overriding Polymorphism in Java Method Overloading and Method Overriding S.R JVM assembly Instruction Purpose Type of binding 1. Invokestatic Is used to invoke static method Static 2. Invokespecial Is used to invoke- 1- constructor 2- private non static methods 3- non private, non static method using super 4- final method Static 3. Invokevirtual Is used to invoke Instance method ( non private, non static without super) Dynamic 4. invokeinterface Is used to invoke interface method Dynamic
  • 6. Note:- To see assembly instruction in class file:- Syntax:- javap -c classFileName Example:- javap -c Calculate   Method overloading Writing two or more method in the same class in such a way that each method has same name but with different method signature ( by varying there number of argument or varying the type of argument or varying the order of the argument) is called method overloading. Method overloading is one of the means of implementing polymorphism.
  • 7. Example- public classCalculate { void sum(int a, int b) { System.out.println("Sum of two number="+(a+b)); } void sum(inta,int b, int c) { System.out.println("Sum of ThreeNumber="+(a+b+c)); } public static void main(String args[]) { Calculatex=new Calculate(); x.sum(2,3); x.sum(2,3,4); } } Output: Sum of two number=5 Sum of ThreeNumber=9 Note- Herepolymorphism isimplemented in sameclass.
  • 8. Method Overriding Writing two or more method in super and sub class such that the method have same name and same signature is called method overriding. Method overriding is one of the means of implementing polymorphism. Example-
  • 9. Programmethodoverriding classCalculate { void sum(int a, int b) { System.out.println("Sum of two number="+(a+b)); } } classUseextendsCalculate { void sum(int a, int b) { System.out.println("Sum of Two Number (overloaded method)="+(a+b)); } public static void main(String args[]) { Usex=new Use(); x.sum(2,3); } } Output: Sum of Two Number (overloaded method)=5 Note- Herepolymorphism isimplemented in super and sub class.
  • 10. WWW.JAVATPORTAL.COM India's No. one Tutorials and a Solution of all Technology Address:- Sector 58 Noida, Uttar Pradesh 201301, India. Mobile: +91 9458548058, 8860449650 Email: sales.javatportal@gmail.com Website: http://www.javatportal.com