SlideShare a Scribd company logo
Copyright @ 2015 Learntek. All Rights Reserved. 1
Polymorphism in Java
Copyright @ 2018 Learntek. All Rights Reserved. 3
What is Polymorphism?
Polymorphism is the ability of an object to take more than one form. It is one of the
important concepts of object-oriented programming language. JAVA is an object-
oriented programming language that supports the concept of polymorphisms.
Alternatively, it is defined as the ability of a reference variable to change behavior
according to what object instance it is holding.
This allows multiple objects of different subclasses to be treated as objects of a single
parent class, while automatically selecting the proper methods to apply to an object
based on the child class it belongs.
Copyright @ 2018 Learntek. All Rights Reserved. 4
There are two types of polymorphism in Java:
1) Compile-time polymorphism (static binding)
2) Runtime polymorphism (dynamic binding)
Method overloading is an example of compile-time polymorphism, while method
overriding is an example of runtime polymorphism.
Copyright @ 2018 Learntek. All Rights Reserved. 5
1. Compile-time Polymorphism:
The type of polymorphism that is implemented when the compiler compiles a
program is called compile-time polymorphism. This type of polymorphism is also
called as static polymorphism or early binding.
Copyright @ 2018 Learntek. All Rights Reserved. 6
Method Overloading:
If a class has multiple methods having the same name but different in parameters, it is
known as Method Overloading. Method overloading is performed within the class.
If we must perform only one operation, having the same name as the methods increase
the readability of the program.
There are three ways to overload the method in java
• By changing the number of arguments
• By changing the data type
• By changing both numbers of arguments and data type
Copyright @ 2018 Learntek. All Rights Reserved. 7
a. Method overloading by changing the number of argument:
Suppose you have to perform addition of the given numbers but there can be any
number of arguments, if you write the method such as x(int, int) for two parameters,
and y(int, int, int) for three parameters then it may be difficult for you as well as other
programmers to understand the behavior of the method because its name differs
Polymorphism in java— Example1:
In the following program, two methods are created, first add1() performs addition of
two numbers and second add1() performs addition of three numbers.
Copyright @ 2018 Learntek. All Rights Reserved. 8
In following example, we are creating static methods so that we don’t need to
create instance for calling methods.
class Addition
{
static int add1 (int a,int b)
{
return a+b;
}
static int add1(int a,int b,int c)
{
return a+b+c;
}
}
Copyright @ 2018 Learntek. All Rights Reserved. 9
class xyz
{
public static void main(String[] args)
{
System.out.println(Addition.add1(13,13));
System.out.println(Addition.add1(9,9,9));
}
}Output:
26
27
Copyright @ 2018 Learntek. All Rights Reserved. 10
b. Method Overloading by changing the data type:
Sometime there is need to use the same method which is having different data
type. In the following program, we have created two methods that differs in data
type. The first add1() method receives two integer arguments and second add1()
method receives two double arguments.
class Addition
{
static int add1 (int a,int b)
{
return a+b;
}
Copyright @ 2018 Learntek. All Rights Reserved. 11
static double add1(double a,double b)
{
return a+b;
}
}
class xyz1
{
public static void main(String[] args)
{
System.out.println(Addition.add1(13,13));
System.out.println(Addition.add1(9.5,9.5));
}
}
Output:
26
19
c. Method Overloading by changing both number of argument and data type:
Sometimes we may need to use the method which is having the different
number of argument and different data type. In the following program, both
number of argument and data type is to be changed. Here the method do() is
overloaded 3 times: first having one int parameter, second one has 2 int
parameters and third one is having double arg. The methods are invoked or
called with the same type and number of parameters used.
Copyright @ 2018 Learntek. All Rights Reserved.
class Overload1
{
void do (int a)
{
System.out.println (“a: ” + a);
}
void do (int a, int b)
{
System.out.println (“a and b: ” + a + “,” + b);
}
double do(double a)
{
System.out.println(“double a: ” + a);
return a*a;
}
}
Copyright @ 2018 Learntek. All Rights Reserved.
class Overload2{
public static void main (String args [])
{
Overload1 Obj = new Overload1();
double result;
Obj.do(20);
Obj.do(20, 30);
result = Obj.do(4.5);
System.out.println(“Output is: ” + result);
}
}a: 20
a and b: 20,30
double a: 4.5
Output is: 20.25
Copyright @ 2018 Learntek. All Rights Reserved.
Copyright @ 2018 Learntek. All Rights Reserved.
2. Run-time Polymorphism :
The type of polymorphism which is implemented dynamically when a program being
executed is called as run-time polymorphism.
The run-time polymorphism is also called dynamic polymorphism or late binding.
Method Overriding in Java
Method overriding is used to achieve runtime polymorphism or dynamic binding. In
method overriding the method must have the same name as in the parent class and
it must have the same parameter as in the parent class. If the subclass (child class)
has the same method as declared in the parent class, it is known as method
overriding in Java. Method overriding occurs in two classes that have IS-A
(inheritance) relationship.
Example 1 of method overriding
In the following program, we have defined the do() method in the subclass as defined
in the parent class but it has some specific implementation. The name and parameter
of the method are the same, and there is IS-A relationship between the classes, so
there is method overriding.
//parent class
class Animal
{
//define method
void do()
{
System.out.println(“Animal is eating food”);
}
}
Copyright @ 2018 Learntek. All Rights Reserved.
//Create child class
class dog extends Animal
{
//define the same method as in the parent class
void do()
{
System.out.println(“Dog is eating food “);
}public static void main(String args[])
{
dog obj = new dog();//creating object
obj.do();//calling method
}
}Output:
Dog is eating food
Copyright @ 2018 Learntek. All Rights Reserved.
Example 2 of Method Overriding
An example or program is given below from the real-world where Vehicle is parent
class. It can have Vehicle class and its specialized child classes like bike and car. These
subclasses will override the default behavior provided by Vehicle class and some of its
own specific behavior.
public class Vehicle
{
public void run()
{
System.out.println(“some speed”);
}
}class bike extends Vehicle
{
Copyright @ 2018 Learntek. All Rights Reserved.
public void run()
{
System.out.println(“speed is slower than car”);
}
}class car extends Vehicle
{
public void run()
{
System.out.println(“speed is faster than bike”);
}
}
// Next, run() method will be called, depends on the type of actual instance
Copyright @ 2018 Learntek. All Rights Reserved.
created on runtime public class Demo
{
public static void main(String[] args)
{
Vehicle v1 = new bike();
v1.run();
Vehicle v2 = new car();
v2.run();
}
}Output:
speed is faster than a bike
speed is slower than car
Copyright @ 2018 Learntek. All Rights Reserved.
Advantage of polymorphism:
• It helps programmers to reuse the code, classes, methods written once,
tested and implemented. They may be reused in many ways.
• The single variable name can be used to store variables of multiple data
types such as Int, Float, double, Long, etc).
• Polymorphism helps in reducing the coupling between different
functionalities.
• Method overloading can be implemented on constructors allowing
different ways to initialize objects of a class. This enables you to define
multiple constructors for handling different types of initializations.
• Method overriding works together with inheritance to enable the code-
reuse of existing classes without the need for re-compilation.
Copyright @ 2018 Learntek. All Rights Reserved.
The disadvantage of polymorphism:
• One of the main disadvantages of polymorphism is that developers find it
difficult to implement polymorphism in codes.
• Run time polymorphism can lead to the performance issue where the
machine needs to decide which method or variable to invoke so it basically
degrades the performances as decisions are taken at run time.
• Polymorphism reduces the readability of the program. One needs to
identify the runtime behavior of the program to identify actual execution
time.
Copyright @ 2018 Learntek. All Rights Reserved.
Copyright @ 2018 Learntek. All Rights Reserved. 23
For more Training Information , Contact Us
Email : info@learntek.org
USA : +1734 418 2465
INDIA : +40 4018 1306
+7799713624
Ad

More Related Content

What's hot (20)

Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 
Wrapper class
Wrapper classWrapper class
Wrapper class
kamal kotecha
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
Janu Jahnavi
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
Spotle.ai
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
CharthaGaglani
 
Methods in java
Methods in javaMethods in java
Methods in java
chauhankapil
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
CPD INDIA
 
Static Members-Java.pptx
Static Members-Java.pptxStatic Members-Java.pptx
Static Members-Java.pptx
ADDAGIRIVENKATARAVIC
 
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
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
Lovely Professional University
 
Database Access With JDBC
Database Access With JDBCDatabase Access With JDBC
Database Access With JDBC
Dharani Kumar Madduri
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
Md.Al-imran Roton
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
Nilesh Dalvi
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
Ahsan Raja
 
Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
backdoor
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
Janu Jahnavi
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
Spotle.ai
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
CharthaGaglani
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
CPD INDIA
 
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
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
Md.Al-imran Roton
 
Exceptions in Java
Exceptions in JavaExceptions in Java
Exceptions in Java
Vadym Lotar
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
Nilesh Dalvi
 
Polymorphism presentation in java
Polymorphism presentation in javaPolymorphism presentation in java
Polymorphism presentation in java
Ahsan Raja
 

Similar to Polymorphism in java (20)

Polymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh SarkarPolymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh Sarkar
Animesh Sarkar
 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptx
SajidTk2
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Akhil Mittal
 
Lecture5_Method_overloading_Final power point presentation
Lecture5_Method_overloading_Final power point presentationLecture5_Method_overloading_Final power point presentation
Lecture5_Method_overloading_Final power point presentation
bhargavi804095
 
Lecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaionLecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaion
bhargavi804095
 
Lecture_5_Method_overloading_Final.pptx in Java
Lecture_5_Method_overloading_Final.pptx in JavaLecture_5_Method_overloading_Final.pptx in Java
Lecture_5_Method_overloading_Final.pptx in Java
ssuser5d6130
 
oblect oriented programming language in java notes .pdf
oblect oriented programming language in java  notes .pdfoblect oriented programming language in java  notes .pdf
oblect oriented programming language in java notes .pdf
sanraku980
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
Ajay Chimmani
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
Inocentshuja Ahmad
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
ilias ahmed
 
Chapter 04 Object Oriented programming .pptx
Chapter 04 Object Oriented programming .pptxChapter 04 Object Oriented programming .pptx
Chapter 04 Object Oriented programming .pptx
fikadumeuedu
 
Object Oriented Programming notes provided
Object Oriented Programming notes providedObject Oriented Programming notes provided
Object Oriented Programming notes provided
dummydoona
 
11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf11.Object Oriented Programming.pdf
11.Object Oriented Programming.pdf
Export Promotion Bureau
 
Design patterns
Design patternsDesign patterns
Design patterns
Anas Alpure
 
Overloadingmethod
OverloadingmethodOverloadingmethod
Overloadingmethod
baabtra.com - No. 1 supplier of quality freshers
 
Introduction to OOPs second year cse.pptx
Introduction to OOPs second year cse.pptxIntroduction to OOPs second year cse.pptx
Introduction to OOPs second year cse.pptx
solemanhldr
 
12th ip CBSE chapter 4 oop in java notes complete
12th ip CBSE  chapter 4 oop in java notes complete12th ip CBSE  chapter 4 oop in java notes complete
12th ip CBSE chapter 4 oop in java notes complete
Harish Gyanani
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) Introduction
SamuelAnsong6
 
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
 
Polymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh SarkarPolymorphism in Java by Animesh Sarkar
Polymorphism in Java by Animesh Sarkar
Animesh Sarkar
 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptx
SajidTk2
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Akhil Mittal
 
Lecture5_Method_overloading_Final power point presentation
Lecture5_Method_overloading_Final power point presentationLecture5_Method_overloading_Final power point presentation
Lecture5_Method_overloading_Final power point presentation
bhargavi804095
 
Lecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaionLecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaion
bhargavi804095
 
Lecture_5_Method_overloading_Final.pptx in Java
Lecture_5_Method_overloading_Final.pptx in JavaLecture_5_Method_overloading_Final.pptx in Java
Lecture_5_Method_overloading_Final.pptx in Java
ssuser5d6130
 
oblect oriented programming language in java notes .pdf
oblect oriented programming language in java  notes .pdfoblect oriented programming language in java  notes .pdf
oblect oriented programming language in java notes .pdf
sanraku980
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
Ajay Chimmani
 
Presentation 4th
Presentation 4thPresentation 4th
Presentation 4th
Connex
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
Inocentshuja Ahmad
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
ilias ahmed
 
Chapter 04 Object Oriented programming .pptx
Chapter 04 Object Oriented programming .pptxChapter 04 Object Oriented programming .pptx
Chapter 04 Object Oriented programming .pptx
fikadumeuedu
 
Object Oriented Programming notes provided
Object Oriented Programming notes providedObject Oriented Programming notes provided
Object Oriented Programming notes provided
dummydoona
 
Introduction to OOPs second year cse.pptx
Introduction to OOPs second year cse.pptxIntroduction to OOPs second year cse.pptx
Introduction to OOPs second year cse.pptx
solemanhldr
 
12th ip CBSE chapter 4 oop in java notes complete
12th ip CBSE  chapter 4 oop in java notes complete12th ip CBSE  chapter 4 oop in java notes complete
12th ip CBSE chapter 4 oop in java notes complete
Harish Gyanani
 
Object Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) IntroductionObject Oriented Programming (OOP) Introduction
Object Oriented Programming (OOP) Introduction
SamuelAnsong6
 
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
 
Ad

More from sureshraj43 (6)

What is maven
What is mavenWhat is maven
What is maven
sureshraj43
 
Apache kafka
Apache kafkaApache kafka
Apache kafka
sureshraj43
 
Ansible: Simple yet powerful IT automation tool
Ansible: Simple yet powerful IT automation toolAnsible: Simple yet powerful IT automation tool
Ansible: Simple yet powerful IT automation tool
sureshraj43
 
Machine learning and pattern recognition
Machine learning and pattern recognitionMachine learning and pattern recognition
Machine learning and pattern recognition
sureshraj43
 
Python datetime
Python datetimePython datetime
Python datetime
sureshraj43
 
Business analyst tools
Business analyst toolsBusiness analyst tools
Business analyst tools
sureshraj43
 
Ansible: Simple yet powerful IT automation tool
Ansible: Simple yet powerful IT automation toolAnsible: Simple yet powerful IT automation tool
Ansible: Simple yet powerful IT automation tool
sureshraj43
 
Machine learning and pattern recognition
Machine learning and pattern recognitionMachine learning and pattern recognition
Machine learning and pattern recognition
sureshraj43
 
Business analyst tools
Business analyst toolsBusiness analyst tools
Business analyst tools
sureshraj43
 
Ad

Recently uploaded (20)

PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 
PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025PDF Reader Pro Crack Latest Version FREE Download 2025
PDF Reader Pro Crack Latest Version FREE Download 2025
mu394968
 
WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)WinRAR Crack for Windows (100% Working 2025)
WinRAR Crack for Windows (100% Working 2025)
sh607827
 
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
How to Batch Export Lotus Notes NSF Emails to Outlook PST Easily?
steaveroggers
 
Adobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest VersionAdobe Illustrator Crack FREE Download 2025 Latest Version
Adobe Illustrator Crack FREE Download 2025 Latest Version
kashifyounis067
 
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Exceptional Behaviors: How Frequently Are They Tested? (AST 2025)
Andre Hora
 
EASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License CodeEASEUS Partition Master Crack + License Code
EASEUS Partition Master Crack + License Code
aneelaramzan63
 
Automation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath CertificateAutomation Techniques in RPA - UiPath Certificate
Automation Techniques in RPA - UiPath Certificate
VICTOR MAESTRE RAMIREZ
 
Download Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With LatestDownload Wondershare Filmora Crack [2025] With Latest
Download Wondershare Filmora Crack [2025] With Latest
tahirabibi60507
 
Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025Adobe After Effects Crack FREE FRESH version 2025
Adobe After Effects Crack FREE FRESH version 2025
kashifyounis067
 
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Proactive Vulnerability Detection in Source Code Using Graph Neural Networks:...
Ranjan Baisak
 
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Salesforce Data Cloud- Hyperscale data platform, built for Salesforce.
Dele Amefo
 
How to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud PerformanceHow to Optimize Your AWS Environment for Improved Cloud Performance
How to Optimize Your AWS Environment for Improved Cloud Performance
ThousandEyes
 
Solidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license codeSolidworks Crack 2025 latest new + license code
Solidworks Crack 2025 latest new + license code
aneelaramzan63
 
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdfMicrosoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
Microsoft AI Nonprofit Use Cases and Live Demo_2025.04.30.pdf
TechSoup
 
Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025Avast Premium Security Crack FREE Latest Version 2025
Avast Premium Security Crack FREE Latest Version 2025
mu394968
 
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& ConsiderationsDesigning AI-Powered APIs on Azure: Best Practices& Considerations
Designing AI-Powered APIs on Azure: Best Practices& Considerations
Dinusha Kumarasiri
 
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
TestMigrationsInPy: A Dataset of Test Migrations from Unittest to Pytest (MSR...
Andre Hora
 
Not So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java WebinarNot So Common Memory Leaks in Java Webinar
Not So Common Memory Leaks in Java Webinar
Tier1 app
 
Kubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptxKubernetes_101_Zero_to_Platform_Engineer.pptx
Kubernetes_101_Zero_to_Platform_Engineer.pptx
CloudScouts
 
The Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdfThe Significance of Hardware in Information Systems.pdf
The Significance of Hardware in Information Systems.pdf
drewplanas10
 

Polymorphism in java

  • 1. Copyright @ 2015 Learntek. All Rights Reserved. 1
  • 3. Copyright @ 2018 Learntek. All Rights Reserved. 3 What is Polymorphism? Polymorphism is the ability of an object to take more than one form. It is one of the important concepts of object-oriented programming language. JAVA is an object- oriented programming language that supports the concept of polymorphisms. Alternatively, it is defined as the ability of a reference variable to change behavior according to what object instance it is holding. This allows multiple objects of different subclasses to be treated as objects of a single parent class, while automatically selecting the proper methods to apply to an object based on the child class it belongs.
  • 4. Copyright @ 2018 Learntek. All Rights Reserved. 4 There are two types of polymorphism in Java: 1) Compile-time polymorphism (static binding) 2) Runtime polymorphism (dynamic binding) Method overloading is an example of compile-time polymorphism, while method overriding is an example of runtime polymorphism.
  • 5. Copyright @ 2018 Learntek. All Rights Reserved. 5 1. Compile-time Polymorphism: The type of polymorphism that is implemented when the compiler compiles a program is called compile-time polymorphism. This type of polymorphism is also called as static polymorphism or early binding.
  • 6. Copyright @ 2018 Learntek. All Rights Reserved. 6 Method Overloading: If a class has multiple methods having the same name but different in parameters, it is known as Method Overloading. Method overloading is performed within the class. If we must perform only one operation, having the same name as the methods increase the readability of the program. There are three ways to overload the method in java • By changing the number of arguments • By changing the data type • By changing both numbers of arguments and data type
  • 7. Copyright @ 2018 Learntek. All Rights Reserved. 7 a. Method overloading by changing the number of argument: Suppose you have to perform addition of the given numbers but there can be any number of arguments, if you write the method such as x(int, int) for two parameters, and y(int, int, int) for three parameters then it may be difficult for you as well as other programmers to understand the behavior of the method because its name differs Polymorphism in java— Example1: In the following program, two methods are created, first add1() performs addition of two numbers and second add1() performs addition of three numbers.
  • 8. Copyright @ 2018 Learntek. All Rights Reserved. 8 In following example, we are creating static methods so that we don’t need to create instance for calling methods. class Addition { static int add1 (int a,int b) { return a+b; } static int add1(int a,int b,int c) { return a+b+c; } }
  • 9. Copyright @ 2018 Learntek. All Rights Reserved. 9 class xyz { public static void main(String[] args) { System.out.println(Addition.add1(13,13)); System.out.println(Addition.add1(9,9,9)); } }Output: 26 27
  • 10. Copyright @ 2018 Learntek. All Rights Reserved. 10 b. Method Overloading by changing the data type: Sometime there is need to use the same method which is having different data type. In the following program, we have created two methods that differs in data type. The first add1() method receives two integer arguments and second add1() method receives two double arguments. class Addition { static int add1 (int a,int b) { return a+b; }
  • 11. Copyright @ 2018 Learntek. All Rights Reserved. 11 static double add1(double a,double b) { return a+b; } } class xyz1 { public static void main(String[] args) { System.out.println(Addition.add1(13,13)); System.out.println(Addition.add1(9.5,9.5)); } }
  • 12. Output: 26 19 c. Method Overloading by changing both number of argument and data type: Sometimes we may need to use the method which is having the different number of argument and different data type. In the following program, both number of argument and data type is to be changed. Here the method do() is overloaded 3 times: first having one int parameter, second one has 2 int parameters and third one is having double arg. The methods are invoked or called with the same type and number of parameters used. Copyright @ 2018 Learntek. All Rights Reserved.
  • 13. class Overload1 { void do (int a) { System.out.println (“a: ” + a); } void do (int a, int b) { System.out.println (“a and b: ” + a + “,” + b); } double do(double a) { System.out.println(“double a: ” + a); return a*a; } } Copyright @ 2018 Learntek. All Rights Reserved.
  • 14. class Overload2{ public static void main (String args []) { Overload1 Obj = new Overload1(); double result; Obj.do(20); Obj.do(20, 30); result = Obj.do(4.5); System.out.println(“Output is: ” + result); } }a: 20 a and b: 20,30 double a: 4.5 Output is: 20.25 Copyright @ 2018 Learntek. All Rights Reserved.
  • 15. Copyright @ 2018 Learntek. All Rights Reserved. 2. Run-time Polymorphism : The type of polymorphism which is implemented dynamically when a program being executed is called as run-time polymorphism. The run-time polymorphism is also called dynamic polymorphism or late binding. Method Overriding in Java Method overriding is used to achieve runtime polymorphism or dynamic binding. In method overriding the method must have the same name as in the parent class and it must have the same parameter as in the parent class. If the subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java. Method overriding occurs in two classes that have IS-A (inheritance) relationship.
  • 16. Example 1 of method overriding In the following program, we have defined the do() method in the subclass as defined in the parent class but it has some specific implementation. The name and parameter of the method are the same, and there is IS-A relationship between the classes, so there is method overriding. //parent class class Animal { //define method void do() { System.out.println(“Animal is eating food”); } } Copyright @ 2018 Learntek. All Rights Reserved.
  • 17. //Create child class class dog extends Animal { //define the same method as in the parent class void do() { System.out.println(“Dog is eating food “); }public static void main(String args[]) { dog obj = new dog();//creating object obj.do();//calling method } }Output: Dog is eating food Copyright @ 2018 Learntek. All Rights Reserved.
  • 18. Example 2 of Method Overriding An example or program is given below from the real-world where Vehicle is parent class. It can have Vehicle class and its specialized child classes like bike and car. These subclasses will override the default behavior provided by Vehicle class and some of its own specific behavior. public class Vehicle { public void run() { System.out.println(“some speed”); } }class bike extends Vehicle { Copyright @ 2018 Learntek. All Rights Reserved.
  • 19. public void run() { System.out.println(“speed is slower than car”); } }class car extends Vehicle { public void run() { System.out.println(“speed is faster than bike”); } } // Next, run() method will be called, depends on the type of actual instance Copyright @ 2018 Learntek. All Rights Reserved.
  • 20. created on runtime public class Demo { public static void main(String[] args) { Vehicle v1 = new bike(); v1.run(); Vehicle v2 = new car(); v2.run(); } }Output: speed is faster than a bike speed is slower than car Copyright @ 2018 Learntek. All Rights Reserved.
  • 21. Advantage of polymorphism: • It helps programmers to reuse the code, classes, methods written once, tested and implemented. They may be reused in many ways. • The single variable name can be used to store variables of multiple data types such as Int, Float, double, Long, etc). • Polymorphism helps in reducing the coupling between different functionalities. • Method overloading can be implemented on constructors allowing different ways to initialize objects of a class. This enables you to define multiple constructors for handling different types of initializations. • Method overriding works together with inheritance to enable the code- reuse of existing classes without the need for re-compilation. Copyright @ 2018 Learntek. All Rights Reserved.
  • 22. The disadvantage of polymorphism: • One of the main disadvantages of polymorphism is that developers find it difficult to implement polymorphism in codes. • Run time polymorphism can lead to the performance issue where the machine needs to decide which method or variable to invoke so it basically degrades the performances as decisions are taken at run time. • Polymorphism reduces the readability of the program. One needs to identify the runtime behavior of the program to identify actual execution time. Copyright @ 2018 Learntek. All Rights Reserved.
  • 23. Copyright @ 2018 Learntek. All Rights Reserved. 23 For more Training Information , Contact Us Email : [email protected] USA : +1734 418 2465 INDIA : +40 4018 1306 +7799713624