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

PPTX
JAVA AWT
PPT
Java collections concept
PPTX
Method overloading
PPTX
INHERITANCE IN JAVA.pptx
PPTX
Multithread Programing in Java
PPTX
Abstract class and Interface
PDF
Java threads
PPTX
Php string function
JAVA AWT
Java collections concept
Method overloading
INHERITANCE IN JAVA.pptx
Multithread Programing in Java
Abstract class and Interface
Java threads
Php string function

What's hot (20)

PPTX
java 8 new features
PDF
Collections Api - Java
PPTX
Java RMI
PDF
Java Course 8: I/O, Files and Streams
PPT
Abstract class in java
PPTX
Core java complete ppt(note)
PPTX
java interface and packages
PPT
Method overriding
PPTX
Java awt (abstract window toolkit)
PDF
Polymorphism In Java
PPTX
Introduction to java
PPTX
Methods in java
PPTX
Polymorphism in java
PPS
Java Exception handling
PDF
Arrays in Java
PPTX
Exception handling in java.pptx
PDF
INTEGRATION TESTING
PPTX
C++ decision making
PDF
Generics
java 8 new features
Collections Api - Java
Java RMI
Java Course 8: I/O, Files and Streams
Abstract class in java
Core java complete ppt(note)
java interface and packages
Method overriding
Java awt (abstract window toolkit)
Polymorphism In Java
Introduction to java
Methods in java
Polymorphism in java
Java Exception handling
Arrays in Java
Exception handling in java.pptx
INTEGRATION TESTING
C++ decision making
Generics
Ad

Viewers also liked (20)

PDF
Polymorphism
PPTX
Polymorphism
PPT
Introduction to method overloading & method overriding in java hdm
PPTX
Inheritance and Polymorphism Java
PPTX
Polymorphism
PPT
JAVA Polymorphism
PPTX
Polymorphism presentation in java
PPT
Java: Inheritance
PPTX
polymorphism
PPTX
Inheritance in JAVA PPT
PPT
Polymorphism
PPTX
DOC
Danelle Williams Resume_Healthcare Administration (2)
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPTX
2CPP11 - Method Overloading
PPTX
2CPP08 - Overloading and Overriding
PPT
Oop Constructor Destructors Constructor Overloading lecture 2
PDF
Java API for XML Web Services (JAX-WS)
PPTX
Mutual fund
PPT
Java Arrays
Polymorphism
Polymorphism
Introduction to method overloading & method overriding in java hdm
Inheritance and Polymorphism Java
Polymorphism
JAVA Polymorphism
Polymorphism presentation in java
Java: Inheritance
polymorphism
Inheritance in JAVA PPT
Polymorphism
Danelle Williams Resume_Healthcare Administration (2)
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
2CPP11 - Method Overloading
2CPP08 - Overloading and Overriding
Oop Constructor Destructors Constructor Overloading lecture 2
Java API for XML Web Services (JAX-WS)
Mutual fund
Java Arrays
Ad

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

PPTX
OOP- PolymorphismFinal12injavait101.pptx
PPTX
JAVA_POLYMORPHISM.pptx
PPTX
Chapter 4
PPTX
Polymorphism in java
PPTX
Basics of polymorphism underlying hhhhhhh
PPT
Polymorphism in java
PDF
Polymorphism in Java
PPTX
Polymorphism
PPT
Polymorphism
PDF
Polymorphism in Java by Animesh Sarkar
PDF
Java Polymorphism: Types And Examples (Geekster)
PDF
Unit 2 Part 1 POLYMORPHISM.pdf
PPTX
Method Overloading in Java
PPTX
Java(Polymorphism)
PPTX
java poly ppt.pptx
PPTX
Polymorphism
PDF
Learn java lessons_online
PPTX
Method Overloading in Java
PPTX
Polymorphism.pptx
OOP- PolymorphismFinal12injavait101.pptx
JAVA_POLYMORPHISM.pptx
Chapter 4
Polymorphism in java
Basics of polymorphism underlying hhhhhhh
Polymorphism in java
Polymorphism in Java
Polymorphism
Polymorphism
Polymorphism in Java by Animesh Sarkar
Java Polymorphism: Types And Examples (Geekster)
Unit 2 Part 1 POLYMORPHISM.pdf
Method Overloading in Java
Java(Polymorphism)
java poly ppt.pptx
Polymorphism
Learn java lessons_online
Method Overloading in Java
Polymorphism.pptx

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Pre independence Education in Inndia.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
master seminar digital applications in india
PDF
Insiders guide to clinical Medicine.pdf
Cell Structure & Organelles in detailed.
102 student loan defaulters named and shamed – Is someone you know on the list?
Pre independence Education in Inndia.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Anesthesia in Laparoscopic Surgery in India
O5-L3 Freight Transport Ops (International) V1.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Microbial diseases, their pathogenesis and prophylaxis
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Module 4: Burden of Disease Tutorial Slides S2 2025
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Renaissance Architecture: A Journey from Faith to Humanism
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Basic Mud Logging Guide for educational purpose
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Microbial disease of the cardiovascular and lymphatic systems
master seminar digital applications in india
Insiders guide to clinical Medicine.pdf

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