SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
Polymorphism in java
Polymorphism:-
It is the ability of an object to take on many forms.
In java language, polymorphism is essentially considered into two versions.
• Compile time polymorphism (method overloading/ static binding )
• Runtime polymorphism (method overriding/ dynamic binding )
Compile time polymorphism (method overloading):-
This is used to write the program in such a way, that flow of control is decided in compile
time itself. It is achieved using method overloading(implicitly).
In method overloading, an object can have two or more methods with same name but with
their method parameters different. These parameters may be different on two bases:
 Parameter type: Type of method parameters can be different.
public void num(double a, double b){..}
public void num (float a, float b){..}
public void num (int a, int b){..}
 Parameter count: Functions accepting different number of parameters.
EmployeeFactory.create(String firstName, String lastName){...}
EmployeeFactory.create(Integer id, String firstName, String lastName){...}
o Both methods have same name “create” but actual method invoked will be
based on parameters passed in program.
Runtime polymorphism (method overriding):-
Feature that allows a subclass or child class to provide a specific implementation of
a method that is already provided by one of its super classes or parent classes.
public class students {
public void study(){
System.out.println(“OOP"); }
}
class Ghazanfar extends students{
public void study(){
System.out.println(“OOP,Discrete"); }
}
class Rafah extends students{
public void study(){
System.out.println(“Report writing"); }
}
Now which study() method will be called?
Depends on type of actual instance created on runtime.
public class Demo
{
public static void main(String[] args) {
students a1 = new Ghazanfar();
a1.study();
students a2 = new rafah();
a2.study();
}
}
ABSTRACTION:-
 Abstraction means hiding the implementation details and showing only the
functionality.
 Abstraction is the process of abstraction in Java is used to hide certain details and
only show the essential features of the object.
Abstract keyword
 Abstract keyword is used to declare the method or class as abstract.
 You have to place the abstract keyword before the method or class name in the
method declaration.
 An abstract method contains a method signature, but no method body.
 Instead of curly braces an abstract method will have a semi colon ( ; ) at the end.
Abstract Class :
A class which contains the abstract keyword in its declaration is known as abstract class.
Syntax:
abstract class <class-name>{}
 An abstract class is something which is incomplete and you cannot create instance of
abstract class.
 If you want to use it you need to make it complete or concrete by extending it.
 A class is called concrete if it does not contain any abstract method and implements
all abstract method inherited from abstract class or interface it has implemented or
extended.
Abstract Methods:
 If we want a class to contain a particular method but we want the actual
implementation of that method to be determined by child classes, we can declare
the method in the parent class as abstract.
 Abstract methods do not have body, they just have prototype(method signature).
 Abstract methods must be implemented in the child class (if the class is not
abstract) otherwise program will throw compilation error
Syntax:
abstract return type method name ();
 An abstract method in Java doesn't have body, it’s just a declaration. In order to
use abstract method you need to override that method in Subclass.
 A method that is declare as abstract and does not have implementation is known
as abstract method.
If you define abstract method than class must be abstract.
package program;
//abstract class
abstract class Sum{
//abstract methods
public abstract int SumOfTwo(int n1, int n2);
public abstract int SumOfThree(int n1, int n2, int n3);
//Regular method
public void disp(){ System.out.println("Method of class Sum");
} }
class AbstractDemo extends Sum{
public int SumOfTwo(int num1, int num2){
return num1+num2;
}
public int SumOfThree(int num1, int num2, int num3){
return num1+num2+num3;
}
public static void main(String args[]){
AbstractDemo obj = new AbstractDemo();
System.out.println(obj.SumOfTwo(3, 7));
System.out.println(obj.SumOfThree(4, 3, 19));
obj.disp();
} }
package presentation;
//Interface
interface Multiply{
//abstract methods
public abstract int multiplyTwo(int n1, int n2);
int multiplyThree(int n1, int n2, int n3);
}
class AbstractDemo2 implements Multiply{
public int multiplyTwo(int num1, int num2){
return num1*num2; }
public int multiplyThree(int num1, int num2, int num3){
return num1*num2*num3; }
public static void main(String args[]){
AbstractDemo2 obj = new AbstractDemo2();
System.out.println(obj.multiplyTwo(3, 7));
System.out.println(obj.multiplyThree(1, 9, 0)); } }
Polymorphism presentation in java

More Related Content

PDF
Polymorphism In Java
PPTX
Java(Polymorphism)
PPTX
Graph traversals in Data Structures
PPTX
APRIORI ALGORITHM -PPT.pptx
PPT
Java collections concept
PPTX
Basic economics ppt
PDF
Railway management system, database mini project
PPTX
Carbon nanotubes ppt
Polymorphism In Java
Java(Polymorphism)
Graph traversals in Data Structures
APRIORI ALGORITHM -PPT.pptx
Java collections concept
Basic economics ppt
Railway management system, database mini project
Carbon nanotubes ppt

What's hot (20)

PPTX
Polymorphism in java
PPTX
Inheritance in java
PPTX
Polymorphism
PPTX
Method overloading
PPT
Abstract class in java
PPTX
Java abstract class & abstract methods
PDF
Method Overloading In Java
PPTX
Applets in java
PPTX
Classes, objects in JAVA
PPT
Introduction to method overloading &amp; method overriding in java hdm
PPTX
Constructor overloading & method overloading
PPTX
Classes objects in java
PPTX
Access specifier
PDF
Java threads
PPTX
Packages in java
PPTX
Control statements in java
PPT
Function overloading(c++)
PPTX
oops concept in java | object oriented programming in java
PPTX
Type casting in java
PPS
Interface
Polymorphism in java
Inheritance in java
Polymorphism
Method overloading
Abstract class in java
Java abstract class & abstract methods
Method Overloading In Java
Applets in java
Classes, objects in JAVA
Introduction to method overloading &amp; method overriding in java hdm
Constructor overloading & method overloading
Classes objects in java
Access specifier
Java threads
Packages in java
Control statements in java
Function overloading(c++)
oops concept in java | object oriented programming in java
Type casting in java
Interface
Ad

Viewers also liked (20)

PPT
Polymorphism in java, method overloading and method overriding
PPTX
polymorphism
PPTX
Polymorphism
PPTX
Polymorphism
PDF
Polymorphism
PDF
Javapolymorphism
PPSX
Swt vs swing
PPT
Oop Constructor Destructors Constructor Overloading lecture 2
PPTX
Inheritance and Polymorphism Java
PPTX
Abstraction in java
PPTX
Abstract class and Interface
PDF
Java Collections API
PPT
Java inheritance
PPT
Exception handling in java
PPT
Java: Inheritance
PPSX
Exception Handling
PDF
Java exception handling ppt
PPSX
PPT
Java awt
PPTX
Exception handling
Polymorphism in java, method overloading and method overriding
polymorphism
Polymorphism
Polymorphism
Polymorphism
Javapolymorphism
Swt vs swing
Oop Constructor Destructors Constructor Overloading lecture 2
Inheritance and Polymorphism Java
Abstraction in java
Abstract class and Interface
Java Collections API
Java inheritance
Exception handling in java
Java: Inheritance
Exception Handling
Java exception handling ppt
Java awt
Exception handling
Ad

Similar to Polymorphism presentation in java (20)

PDF
Exception handling and packages.pdf
PPTX
Java Unit 2(part 3)
PPTX
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
PDF
java-06inheritance
PPTX
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
PPTX
Abstraction encapsulation inheritance polymorphism
PDF
Java 06
PPTX
Application package
PPTX
PDF
Abstraction in Java: Abstract class and Interfaces
DOCX
Java notes
PPTX
Lecture5_Method_overloading_Final power point presentation
PPTX
Lecture4_Method_overloading power point presentaion
PPTX
Lecture_5_Method_overloading_Final.pptx in Java
PPTX
Polymorphism in C# Function overloading in C#
PDF
Unit 4 notes.pdf
PDF
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
PPTX
Inheritance
PPTX
Java basics
PPTX
Lecture 8 abstract class and interface
Exception handling and packages.pdf
Java Unit 2(part 3)
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
java-06inheritance
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
Abstraction encapsulation inheritance polymorphism
Java 06
Application package
Abstraction in Java: Abstract class and Interfaces
Java notes
Lecture5_Method_overloading_Final power point presentation
Lecture4_Method_overloading power point presentaion
Lecture_5_Method_overloading_Final.pptx in Java
Polymorphism in C# Function overloading in C#
Unit 4 notes.pdf
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
Inheritance
Java basics
Lecture 8 abstract class and interface

Recently uploaded (20)

PPTX
How to Manage Global Discount in Odoo 18 POS
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
UTS Health Student Promotional Representative_Position Description.pdf
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PPTX
How to Manage Bill Control Policy in Odoo 18
PPTX
An introduction to Dialogue writing.pptx
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
PDF
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
PPTX
IMMUNIZATION PROGRAMME pptx
PPTX
Strengthening open access through collaboration: building connections with OP...
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Odoo 18 Sales_ Managing Quotation Validity
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
How to Manage Global Discount in Odoo 18 POS
NOI Hackathon - Summer Edition - GreenThumber.pptx
Open Quiz Monsoon Mind Game Final Set.pptx
UTS Health Student Promotional Representative_Position Description.pdf
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
How to Manage Bill Control Policy in Odoo 18
An introduction to Dialogue writing.pptx
The Final Stretch: How to Release a Game and Not Die in the Process.
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
IMMUNIZATION PROGRAMME pptx
Strengthening open access through collaboration: building connections with OP...
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
Week 4 Term 3 Study Techniques revisited.pptx
Odoo 18 Sales_ Managing Quotation Validity
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
UPPER GASTRO INTESTINAL DISORDER.docx
Information Texts_Infographic on Forgetting Curve.pptx
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture

Polymorphism presentation in java

  • 2. Polymorphism:- It is the ability of an object to take on many forms. In java language, polymorphism is essentially considered into two versions. • Compile time polymorphism (method overloading/ static binding ) • Runtime polymorphism (method overriding/ dynamic binding )
  • 3. Compile time polymorphism (method overloading):- This is used to write the program in such a way, that flow of control is decided in compile time itself. It is achieved using method overloading(implicitly). In method overloading, an object can have two or more methods with same name but with their method parameters different. These parameters may be different on two bases:  Parameter type: Type of method parameters can be different. public void num(double a, double b){..} public void num (float a, float b){..} public void num (int a, int b){..}  Parameter count: Functions accepting different number of parameters. EmployeeFactory.create(String firstName, String lastName){...} EmployeeFactory.create(Integer id, String firstName, String lastName){...} o Both methods have same name “create” but actual method invoked will be based on parameters passed in program.
  • 4. Runtime polymorphism (method overriding):- Feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super classes or parent classes. public class students { public void study(){ System.out.println(“OOP"); } } class Ghazanfar extends students{ public void study(){ System.out.println(“OOP,Discrete"); } } class Rafah extends students{ public void study(){ System.out.println(“Report writing"); } } Now which study() method will be called?
  • 5. Depends on type of actual instance created on runtime. public class Demo { public static void main(String[] args) { students a1 = new Ghazanfar(); a1.study(); students a2 = new rafah(); a2.study(); } }
  • 6. ABSTRACTION:-  Abstraction means hiding the implementation details and showing only the functionality.  Abstraction is the process of abstraction in Java is used to hide certain details and only show the essential features of the object. Abstract keyword  Abstract keyword is used to declare the method or class as abstract.  You have to place the abstract keyword before the method or class name in the method declaration.  An abstract method contains a method signature, but no method body.  Instead of curly braces an abstract method will have a semi colon ( ; ) at the end.
  • 7. Abstract Class : A class which contains the abstract keyword in its declaration is known as abstract class. Syntax: abstract class <class-name>{}  An abstract class is something which is incomplete and you cannot create instance of abstract class.  If you want to use it you need to make it complete or concrete by extending it.  A class is called concrete if it does not contain any abstract method and implements all abstract method inherited from abstract class or interface it has implemented or extended.
  • 8. Abstract Methods:  If we want a class to contain a particular method but we want the actual implementation of that method to be determined by child classes, we can declare the method in the parent class as abstract.  Abstract methods do not have body, they just have prototype(method signature).  Abstract methods must be implemented in the child class (if the class is not abstract) otherwise program will throw compilation error Syntax: abstract return type method name ();  An abstract method in Java doesn't have body, it’s just a declaration. In order to use abstract method you need to override that method in Subclass.  A method that is declare as abstract and does not have implementation is known as abstract method. If you define abstract method than class must be abstract.
  • 9. package program; //abstract class abstract class Sum{ //abstract methods public abstract int SumOfTwo(int n1, int n2); public abstract int SumOfThree(int n1, int n2, int n3); //Regular method public void disp(){ System.out.println("Method of class Sum"); } } class AbstractDemo extends Sum{ public int SumOfTwo(int num1, int num2){ return num1+num2; } public int SumOfThree(int num1, int num2, int num3){ return num1+num2+num3; } public static void main(String args[]){ AbstractDemo obj = new AbstractDemo(); System.out.println(obj.SumOfTwo(3, 7)); System.out.println(obj.SumOfThree(4, 3, 19)); obj.disp(); } }
  • 10. package presentation; //Interface interface Multiply{ //abstract methods public abstract int multiplyTwo(int n1, int n2); int multiplyThree(int n1, int n2, int n3); } class AbstractDemo2 implements Multiply{ public int multiplyTwo(int num1, int num2){ return num1*num2; } public int multiplyThree(int num1, int num2, int num3){ return num1*num2*num3; } public static void main(String args[]){ AbstractDemo2 obj = new AbstractDemo2(); System.out.println(obj.multiplyTwo(3, 7)); System.out.println(obj.multiplyThree(1, 9, 0)); } }