SlideShare a Scribd company logo
2
Most read
8
Most read
12
Most read
Polymorphism in Java
Polymorphism in Java
• Polymorphism in java is a concept by which
we can perform a single action by different
ways. Polymorphism is derived from 2 greek
words: poly and morphs. The word "poly"
means many and "morphs" means forms. So
polymorphism means many forms.
•
• There are two types of polymorphism in java:
compile time polymorphism and runtime
polymorphism. We can perform
polymorphism in java by method overloading
and method overriding.
• If you overload static method in java, it is the
example of compile time polymorphism.
Runtime Polymorphism in Java
• Runtime polymorphism or Dynamic Method
Dispatch is a process in which a call to an
overridden method is resolved at runtime
rather than compile-time.
• In this process, an overridden method is called
through the reference variable of a superclass.
The determination of the method to be called
is based on the object being referred to by the
reference variable.
Upcasting
• When reference variable of Parent class refers
to the object of Child class, it is known as
upcasting. For example:
Polymorphism in java
• class A
• {}
• class B extends A{}
• A a=new B();//upcasting
Example of Java Runtime Polymorphism
class Bike{
void run(){System.out.println("running");}
}
class Splender extends Bike{
void run()
{System.out.println("running safely with 60km");}
public static void main(String args[]){
Bike b = new Splender();//upcasting
b.run();
} }
Java Runtime Polymorphism with Data
Member
• Method is overridden not the datamembers,
so runtime polymorphism can't be achieved
by data members.
• class Bike{
• int speedlimit=90;
• }
• class Honda3 extends Bike{
• int speedlimit=150;
•
• public static void main(String args[]){
• Bike obj=new Honda3();
• System.out.println(obj.speedlimit);//90
• }
Java Runtime Polymorphism with
Multilevel Inheritance
class Animal{
void eat(){System.out.println("eating");}
}
class Dog extends Animal{
void eat(){System.out.println("eating fruits");}
}
class BabyDog extends Dog{
void eat(){System.out.println("drinking milk");}
public static void main(String args[])
{  
Animal a1,a2,a3;  
a1=new Animal();  
a2=new Dog();  
a3=new BabyDog();  
a1.eat();  
a2.eat();  
a3.eat();  
}  }  

More Related Content

What's hot (20)

PPTX
Variables in C and C++ Language
Way2itech
 
PPTX
Constructor ppt
Vinod Kumar
 
PPTX
Super keyword in java
Hitesh Kumar
 
PPTX
Operators in java
AbhishekMondal42
 
PPTX
Queues in C++
Vineeta Garg
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPS
Wrapper class
kamal kotecha
 
PDF
Method Overloading In Java
CharthaGaglani
 
PPTX
Interface in java
PhD Research Scholar
 
PPT
JAVA Polymorphism
Mahi Mca
 
PDF
Java threads
Prabhakaran V M
 
PPT
Java And Multithreading
Shraddha
 
PPTX
Method overloading
Lovely Professional University
 
PPT
Java Notes
Abhishek Khune
 
PPTX
Constructor overloading & method overloading
garishma bhatia
 
PPTX
Inheritance in java
RahulAnanda1
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPT
Java Streams
M Vishnuvardhan Reddy
 
PPTX
Polymorphism in c++(ppt)
Sanjit Shaw
 
PDF
Java 8 Lambda Expressions
Scott Leberknight
 
Variables in C and C++ Language
Way2itech
 
Constructor ppt
Vinod Kumar
 
Super keyword in java
Hitesh Kumar
 
Operators in java
AbhishekMondal42
 
Queues in C++
Vineeta Garg
 
Constructor in java
Pavith Gunasekara
 
Wrapper class
kamal kotecha
 
Method Overloading In Java
CharthaGaglani
 
Interface in java
PhD Research Scholar
 
JAVA Polymorphism
Mahi Mca
 
Java threads
Prabhakaran V M
 
Java And Multithreading
Shraddha
 
Method overloading
Lovely Professional University
 
Java Notes
Abhishek Khune
 
Constructor overloading & method overloading
garishma bhatia
 
Inheritance in java
RahulAnanda1
 
Classes, objects in JAVA
Abhilash Nair
 
Java Streams
M Vishnuvardhan Reddy
 
Polymorphism in c++(ppt)
Sanjit Shaw
 
Java 8 Lambda Expressions
Scott Leberknight
 

Similar to Polymorphism in java (20)

PPTX
Polymorphism
Ahmed Za'anin
 
PPTX
Basics of polymorphism underlying hhhhhhh
dikshaprabhugvm
 
PPTX
Polymorphism
Ducat India
 
PPTX
Polymorphism in java
Janu Jahnavi
 
PPTX
OOP- PolymorphismFinal12injavait101.pptx
codevincent624
 
PPTX
java poly ppt.pptx
sukhpreetsingh295239
 
PPTX
P.7 media 2 polymorphism
ahmadmuzaqqi
 
PPTX
Polymorphism in java
sureshraj43
 
PPT
Polymorphism in java, method overloading and method overriding
JavaTportal
 
PPTX
JAVA_POLYMORPHISM.pptx
Hitesh Kumar Nath
 
PPTX
Chapter 4
siragezeynu
 
PDF
Www javatpoint-com-corejava-interview-questions-2
ssuserd2d58b
 
PPTX
java polymorphism example program oracal.pptx
pirito8705
 
PPTX
Chapter8:Understanding Polymorphism
It Academy
 
PDF
Java Polymorphism: Types And Examples (Geekster)
Geekster
 
PDF
Polymorphism in Java by Animesh Sarkar
Animesh Sarkar
 
PPTX
Advance Java Polymorphism OOPS CON .pptx
jprince1621
 
PDF
Java Polymorphism
Soba Arjun
 
PDF
Object-Oriented Polymorphism Unleashed
Naresh Chintalcheru
 
PPTX
Polymorphism
Arif Ansari
 
Polymorphism
Ahmed Za'anin
 
Basics of polymorphism underlying hhhhhhh
dikshaprabhugvm
 
Polymorphism
Ducat India
 
Polymorphism in java
Janu Jahnavi
 
OOP- PolymorphismFinal12injavait101.pptx
codevincent624
 
java poly ppt.pptx
sukhpreetsingh295239
 
P.7 media 2 polymorphism
ahmadmuzaqqi
 
Polymorphism in java
sureshraj43
 
Polymorphism in java, method overloading and method overriding
JavaTportal
 
JAVA_POLYMORPHISM.pptx
Hitesh Kumar Nath
 
Chapter 4
siragezeynu
 
Www javatpoint-com-corejava-interview-questions-2
ssuserd2d58b
 
java polymorphism example program oracal.pptx
pirito8705
 
Chapter8:Understanding Polymorphism
It Academy
 
Java Polymorphism: Types And Examples (Geekster)
Geekster
 
Polymorphism in Java by Animesh Sarkar
Animesh Sarkar
 
Advance Java Polymorphism OOPS CON .pptx
jprince1621
 
Java Polymorphism
Soba Arjun
 
Object-Oriented Polymorphism Unleashed
Naresh Chintalcheru
 
Polymorphism
Arif Ansari
 
Ad

More from Lovely Professional University (20)

PPTX
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Lovely Professional University
 
PPTX
Effort Estimation: Meaning, Problems with Estimation, Basis, Estimation Techn...
Lovely Professional University
 
PPTX
Project Approach: Intro. Technical Plan, Choice of Process Models: Waterfall,...
Lovely Professional University
 
PPTX
Programme Management & Project Evaluation
Lovely Professional University
 
PPTX
Step Wise Project Planning: Project Scope, Objectives, Infrastructure, Charac...
Lovely Professional University
 
PPTX
Introduction to Software Project Management:
Lovely Professional University
 
PDF
The HyperText Markup Language or HTML is the standard markup language
Lovely Professional University
 
PPTX
Working with JSON
Lovely Professional University
 
PPTX
NODEMON Module
Lovely Professional University
 
PPTX
Getting Input from User
Lovely Professional University
 
PPTX
fs Module.pptx
Lovely Professional University
 
PPTX
Transaction Processing in DBMS.pptx
Lovely Professional University
 
PPT
web_server_browser.ppt
Lovely Professional University
 
PPTX
Web Server.pptx
Lovely Professional University
 
PPTX
Number System.pptx
Lovely Professional University
 
PPT
Programming Language.ppt
Lovely Professional University
 
PPTX
Information System.pptx
Lovely Professional University
 
PPTX
Applications of Computer Science in Pharmacy-1.pptx
Lovely Professional University
 
PPTX
Application of Computers in Pharmacy.pptx
Lovely Professional University
 
Activity Planning: Objectives, Project Schedule, Network Planning Model. Time...
Lovely Professional University
 
Effort Estimation: Meaning, Problems with Estimation, Basis, Estimation Techn...
Lovely Professional University
 
Project Approach: Intro. Technical Plan, Choice of Process Models: Waterfall,...
Lovely Professional University
 
Programme Management & Project Evaluation
Lovely Professional University
 
Step Wise Project Planning: Project Scope, Objectives, Infrastructure, Charac...
Lovely Professional University
 
Introduction to Software Project Management:
Lovely Professional University
 
The HyperText Markup Language or HTML is the standard markup language
Lovely Professional University
 
Getting Input from User
Lovely Professional University
 
Transaction Processing in DBMS.pptx
Lovely Professional University
 
web_server_browser.ppt
Lovely Professional University
 
Number System.pptx
Lovely Professional University
 
Programming Language.ppt
Lovely Professional University
 
Information System.pptx
Lovely Professional University
 
Applications of Computer Science in Pharmacy-1.pptx
Lovely Professional University
 
Application of Computers in Pharmacy.pptx
Lovely Professional University
 
Ad

Recently uploaded (20)

PPTX
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PPTX
Information Retrieval and Extraction - Module 7
premSankar19
 
PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PPTX
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PPTX
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PDF
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PDF
All chapters of Strength of materials.ppt
girmabiniyam1234
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PPTX
ENSA_Module_7.pptx_wide_area_network_concepts
RanaMukherjee24
 
PDF
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PDF
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
ETP Presentation(1000m3 Small ETP For Power Plant and industry
MD Azharul Islam
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
Information Retrieval and Extraction - Module 7
premSankar19
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
Basics of Auto Computer Aided Drafting .pptx
Krunal Thanki
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
Introduction to Fluid and Thermal Engineering
Avesahemad Husainy
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
4 Tier Teamcenter Installation part1.pdf
VnyKumar1
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
All chapters of Strength of materials.ppt
girmabiniyam1234
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
ENSA_Module_7.pptx_wide_area_network_concepts
RanaMukherjee24
 
Advanced LangChain & RAG: Building a Financial AI Assistant with Real-Time Data
Soufiane Sejjari
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
SG1-ALM-MS-EL-30-0008 (00) MS - Isolators and disconnecting switches.pdf
djiceramil
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 

Polymorphism in java

  • 2. Polymorphism in Java • Polymorphism in java is a concept by which we can perform a single action by different ways. Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means many forms. •
  • 3. • There are two types of polymorphism in java: compile time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding. • If you overload static method in java, it is the example of compile time polymorphism.
  • 4. Runtime Polymorphism in Java • Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time.
  • 5. • In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable.
  • 6. Upcasting • When reference variable of Parent class refers to the object of Child class, it is known as upcasting. For example:
  • 8. • class A • {} • class B extends A{} • A a=new B();//upcasting
  • 9. Example of Java Runtime Polymorphism class Bike{ void run(){System.out.println("running");} } class Splender extends Bike{ void run() {System.out.println("running safely with 60km");} public static void main(String args[]){ Bike b = new Splender();//upcasting b.run(); } }
  • 10. Java Runtime Polymorphism with Data Member • Method is overridden not the datamembers, so runtime polymorphism can't be achieved by data members.
  • 11. • class Bike{ • int speedlimit=90; • } • class Honda3 extends Bike{ • int speedlimit=150; • • public static void main(String args[]){ • Bike obj=new Honda3(); • System.out.println(obj.speedlimit);//90 • }
  • 12. Java Runtime Polymorphism with Multilevel Inheritance class Animal{ void eat(){System.out.println("eating");} } class Dog extends Animal{ void eat(){System.out.println("eating fruits");} } class BabyDog extends Dog{ void eat(){System.out.println("drinking milk");}