SlideShare ist ein Scribd-Unternehmen logo
1 von 10
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

Weitere ähnliche Inhalte

Was ist angesagt?

ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaAtul Sehdev
 
Java Data Types
Java Data TypesJava Data Types
Java Data TypesSpotle.ai
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in javasureshraj43
 
Object oriented programming concepts
Object oriented programming conceptsObject oriented programming concepts
Object oriented programming conceptsrahuld115
 
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 hdmHarshal Misalkar
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and OperatorsMarwa Ali Eissa
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism JavaM. Raihan
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variablesPushpendra Tyagi
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming languageMd.Al-imran Roton
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphismlalithambiga kamaraj
 
Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
OOP - Polymorphism
OOP - PolymorphismOOP - Polymorphism
OOP - PolymorphismMudasir Qazi
 
Method Overloading in Java
Method Overloading in JavaMethod Overloading in Java
Method Overloading in JavaSonya Akter Rupa
 

Was ist angesagt? (20)

ITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in javaITFT-Constants, variables and data types in java
ITFT-Constants, variables and data types in java
 
Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Object oriented programming concepts
Object oriented programming conceptsObject oriented programming concepts
Object oriented programming concepts
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
OOP java
OOP javaOOP java
OOP java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
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
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
Array in c#
Array in c#Array in c#
Array in c#
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
OOP - Polymorphism
OOP - PolymorphismOOP - Polymorphism
OOP - Polymorphism
 
Method Overloading in Java
Method Overloading in JavaMethod Overloading in Java
Method Overloading in Java
 

Andere mochten auch

JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA PolymorphismMahi Mca
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPTPooja Jaiswal
 
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 KuteTushar B Kute
 
2CPP11 - Method Overloading
2CPP11 - Method Overloading2CPP11 - Method Overloading
2CPP11 - Method OverloadingMichael Heron
 
2CPP08 - Overloading and Overriding
2CPP08 - Overloading and Overriding2CPP08 - Overloading and Overriding
2CPP08 - Overloading and OverridingMichael 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 2Abbas 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 javasawarkar17
 

Andere mochten auch (18)

Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
polymorphism
polymorphism polymorphism
polymorphism
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
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
 

Ähnlich wie Polymorphism in java, method overloading and method overriding

P.7 media 2 polymorphism
P.7 media 2 polymorphismP.7 media 2 polymorphism
P.7 media 2 polymorphismahmadmuzaqqi
 
Learn java lessons_online
Learn java lessons_onlineLearn java lessons_online
Learn java lessons_onlinenishajj
 
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 questionsVinay Kumar
 
Corejavainterviewquestions.doc
Corejavainterviewquestions.docCorejavainterviewquestions.doc
Corejavainterviewquestions.docJoyce Thomas
 
Unit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfUnit 2 Part 1 POLYMORPHISM.pdf
Unit 2 Part 1 POLYMORPHISM.pdfArpana 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-2ssuserd2d58b
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and PolymorphismKartikKapgate
 
polymorphism method overloading and overriding .pptx
polymorphism method overloading  and overriding .pptxpolymorphism method overloading  and overriding .pptx
polymorphism method overloading and overriding .pptxthamaraiselvangts441
 
Advance Java Polymorphism OOPS CON .pptx
Advance Java Polymorphism OOPS CON .pptxAdvance Java Polymorphism OOPS CON .pptx
Advance Java Polymorphism OOPS CON .pptxjprince1621
 

Ähnlich wie Polymorphism in java, method overloading and method overriding (20)

Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
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
 
28csharp
28csharp28csharp
28csharp
 
28c
28c28c
28c
 
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
 

Kürzlich hochgeladen

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 

Kürzlich hochgeladen (20)

ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 

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