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
Ad

More Related Content

What's hot (20)

C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
Boopathi K
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
Poojith Chowdhary
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
Ankita Totala
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
Pranali Chaudhari
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Polymorphism In c++
Polymorphism In c++Polymorphism In c++
Polymorphism In c++
Vishesh Jha
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
KrutikaWankhade1
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Ahmed Za'anin
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Elizabeth alexander
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Madishetty Prathibha
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Nochiketa Chakraborty
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Lovely Professional University
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
Janu Jahnavi
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
Abhilash Nair
 
Unary operator overloading
Unary operator overloadingUnary operator overloading
Unary operator overloading
Md. Ashraful Islam
 

Viewers also liked (18)

Polymorphism
PolymorphismPolymorphism
Polymorphism
Raffaele Doti
 
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
Java API
'Gurumukhi' Vaishnav
 
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
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
M. Raihan
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
Mahi Mca
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
Tareq Hasan
 
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
 
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
 
Abstraction in java
Abstraction in javaAbstraction in java
Abstraction in java
sawarkar17
 
Ad

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

Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
Lovely Professional University
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Arif Ansari
 
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
 
OOP- PolymorphismFinal12injavait101.pptx
OOP- PolymorphismFinal12injavait101.pptxOOP- PolymorphismFinal12injavait101.pptx
OOP- PolymorphismFinal12injavait101.pptx
codevincent624
 
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
 
Basics of polymorphism underlying hhhhhhh
Basics of polymorphism  underlying hhhhhhhBasics of polymorphism  underlying hhhhhhh
Basics of polymorphism underlying hhhhhhh
dikshaprabhugvm
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
talha ijaz
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.doc
Joyce Thomas
 
Method overloading and method overriding
Method overloading and method overridingMethod overloading and method overriding
Method overloading and method overriding
RomitRajSingh1
 
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
 
28csharp
28csharp28csharp
28csharp
Sireesh K
 
28c
28c28c
28c
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
 
P.7 media 2 polymorphism
P.7 media 2 polymorphismP.7 media 2 polymorphism
P.7 media 2 polymorphism
ahmadmuzaqqi
 
OOP- PolymorphismFinal12injavait101.pptx
OOP- PolymorphismFinal12injavait101.pptxOOP- PolymorphismFinal12injavait101.pptx
OOP- PolymorphismFinal12injavait101.pptx
codevincent624
 
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
 
Core java interview questions
Core java interview questionsCore java interview questions
Core java interview questions
Vinay Kumar
 
Basics of polymorphism underlying hhhhhhh
Basics of polymorphism  underlying hhhhhhhBasics of polymorphism  underlying hhhhhhh
Basics of polymorphism underlying hhhhhhh
dikshaprabhugvm
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.doc
Joyce Thomas
 
Method overloading and method overriding
Method overloading and method overridingMethod overloading and method overriding
Method overloading and method overriding
RomitRajSingh1
 
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
 
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
 
Ad

Recently uploaded (20)

Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
National Information Standards Organization (NISO)
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 

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: [email protected] Website: http://www.javatportal.com