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

What's hot (20)

PDF
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
PDF
Polymorphism in oop
MustafaIbrahimy
 
PPTX
Abstraction java
MahinImran
 
PPT
Introduction to method overloading &amp; method overriding in java hdm
Harshal Misalkar
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPTX
Polymorphism in java
Elizabeth alexander
 
PPTX
Polymorphism
Nochiketa Chakraborty
 
PPTX
Polymorphism in java
sureshraj43
 
ODP
OOP java
xball977
 
PPTX
Object oriented programming concepts
rahuld115
 
PPT
Abstract class in java
Lovely Professional University
 
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PDF
Class and Objects in Java
Spotle.ai
 
PPTX
Inheritance in java
RahulAnanda1
 
PPS
Interface
kamal kotecha
 
PPT
Core java concepts
Ram132
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPTX
Multithreading in java
Monika Mishra
 
PPT
Oop Presentation
Ghaffar Khan
 
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
Polymorphism in oop
MustafaIbrahimy
 
Abstraction java
MahinImran
 
Introduction to method overloading &amp; method overriding in java hdm
Harshal Misalkar
 
Constructor in java
Pavith Gunasekara
 
Polymorphism in java
Elizabeth alexander
 
Polymorphism
Nochiketa Chakraborty
 
Polymorphism in java
sureshraj43
 
OOP java
xball977
 
Object oriented programming concepts
rahuld115
 
Abstract class in java
Lovely Professional University
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Exception Handling in JAVA
SURIT DATTA
 
Class and Objects in Java
Spotle.ai
 
Inheritance in java
RahulAnanda1
 
Interface
kamal kotecha
 
Core java concepts
Ram132
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Multithreading in java
Monika Mishra
 
Oop Presentation
Ghaffar Khan
 

Viewers also liked (20)

PPT
Polymorphism in java, method overloading and method overriding
JavaTportal
 
PPTX
polymorphism
Imtiaz Hussain
 
PPTX
Polymorphism
Kumar Gaurav
 
PDF
Polymorphism
Raffaele Doti
 
PDF
Javapolymorphism
karthikenlume
 
PPSX
Swt vs swing
Sara Torkey
 
PPT
Oop Constructor Destructors Constructor Overloading lecture 2
Abbas Ajmal
 
PPTX
Inheritance and Polymorphism Java
M. Raihan
 
PPTX
Abstraction in java
sawarkar17
 
PPTX
Abstract class and Interface
Haris Bin Zahid
 
PDF
Java Collections API
Alex Miller
 
PPT
Java inheritance
Arati Gadgil
 
PPT
Exception handling in java
Pratik Soares
 
PPT
Java: Inheritance
Tareq Hasan
 
PPSX
Exception Handling
Reddhi Basu
 
PDF
Java exception handling ppt
JavabynataraJ
 
PPSX
Inheritance
Selvin Josy Bai Somu
 
PPT
Java awt
Arati Gadgil
 
PPTX
Exception handling
Abhishek Pachisia
 
PPS
Java Exception handling
kamal kotecha
 
Polymorphism in java, method overloading and method overriding
JavaTportal
 
polymorphism
Imtiaz Hussain
 
Polymorphism
Kumar Gaurav
 
Polymorphism
Raffaele Doti
 
Javapolymorphism
karthikenlume
 
Swt vs swing
Sara Torkey
 
Oop Constructor Destructors Constructor Overloading lecture 2
Abbas Ajmal
 
Inheritance and Polymorphism Java
M. Raihan
 
Abstraction in java
sawarkar17
 
Abstract class and Interface
Haris Bin Zahid
 
Java Collections API
Alex Miller
 
Java inheritance
Arati Gadgil
 
Exception handling in java
Pratik Soares
 
Java: Inheritance
Tareq Hasan
 
Exception Handling
Reddhi Basu
 
Java exception handling ppt
JavabynataraJ
 
Java awt
Arati Gadgil
 
Exception handling
Abhishek Pachisia
 
Java Exception handling
kamal kotecha
 
Ad

Similar to Polymorphism presentation in java (20)

PDF
Exception handling and packages.pdf
Kp Sharma
 
PPTX
Java Unit 2(part 3)
Dr. SURBHI SAROHA
 
PPTX
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
SAJITHABANUS
 
PDF
java-06inheritance
Arjun Shanka
 
PPTX
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
SakkaravarthiS1
 
PPTX
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
PDF
Java 06
Loida Igama
 
PPTX
Application package
JAYAARC
 
PPTX
Oop in kotlin
Abdul Rahman Masri Attal
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PDF
Abstraction in Java: Abstract class and Interfaces
Jamsher bhanbhro
 
DOCX
Java notes
Upasana Talukdar
 
PPTX
Lecture5_Method_overloading_Final power point presentation
bhargavi804095
 
PPTX
Lecture4_Method_overloading power point presentaion
bhargavi804095
 
PPTX
Lecture_5_Method_overloading_Final.pptx in Java
ssuser5d6130
 
PPTX
Polymorphism in C# Function overloading in C#
Abid Kohistani
 
PDF
Unit 4 notes.pdf
Revathiparamanathan
 
PDF
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
Akhil Mittal
 
PPTX
Inheritance
Mavoori Soshmitha
 
PPTX
Java basics
Shivanshu Purwar
 
Exception handling and packages.pdf
Kp Sharma
 
Java Unit 2(part 3)
Dr. SURBHI SAROHA
 
702641313-CS3391-OBJORIENTEDPS-Unit-2.pptx
SAJITHABANUS
 
java-06inheritance
Arjun Shanka
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
SakkaravarthiS1
 
Abstraction encapsulation inheritance polymorphism
PriyadharshiniG41
 
Java 06
Loida Igama
 
Application package
JAYAARC
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Abstraction in Java: Abstract class and Interfaces
Jamsher bhanbhro
 
Java notes
Upasana Talukdar
 
Lecture5_Method_overloading_Final power point presentation
bhargavi804095
 
Lecture4_Method_overloading power point presentaion
bhargavi804095
 
Lecture_5_Method_overloading_Final.pptx in Java
ssuser5d6130
 
Polymorphism in C# Function overloading in C#
Abid Kohistani
 
Unit 4 notes.pdf
Revathiparamanathan
 
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
Akhil Mittal
 
Inheritance
Mavoori Soshmitha
 
Java basics
Shivanshu Purwar
 
Ad

Recently uploaded (20)

PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
Introduction to Indian Writing in English
Trushali Dodiya
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Controller Request and Response in Odoo18
Celine George
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Horarios de distribución de agua en julio
pegazohn1978
 

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)); } }