SlideShare une entreprise Scribd logo
1  sur  11
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

Contenu connexe

Tendances (20)

Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Method Overloading In Java
Method Overloading In JavaMethod Overloading In Java
Method Overloading In Java
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 
Polymorphism in oop
Polymorphism in oopPolymorphism in oop
Polymorphism in oop
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 

En vedette

Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overridingJavaTportal
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2Abbas Ajmal
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism JavaM. Raihan
 
Abstraction in java
Abstraction in javaAbstraction in java
Abstraction in javasawarkar17
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and InterfaceHaris Bin Zahid
 
Java Collections API
Java Collections APIJava Collections API
Java Collections APIAlex Miller
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaPratik Soares
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 

En vedette (20)

Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overriding
 
polymorphism
polymorphism polymorphism
polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
 
Swt vs swing
Swt vs swingSwt vs swing
Swt vs swing
 
Oop Constructor Destructors Constructor Overloading lecture 2
Oop Constructor  Destructors Constructor Overloading lecture 2Oop Constructor  Destructors Constructor Overloading lecture 2
Oop Constructor Destructors Constructor Overloading lecture 2
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
Abstraction in java
Abstraction in javaAbstraction in java
Abstraction in java
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Java Collections API
Java Collections APIJava Collections API
Java Collections API
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Java awt
Java awtJava awt
Java awt
 
Exception handling
Exception handlingException handling
Exception handling
 

Similaire à Polymorphism presentation in java

Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdfKp Sharma
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesSakkaravarthiS1
 
Application package
Application packageApplication package
Application packageJAYAARC
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and InterfacesJamsher bhanbhro
 
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 presentationbhargavi804095
 
Lecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaionLecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaionbhargavi804095
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Abid Kohistani
 
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...Akhil Mittal
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interfacemanish kumar
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)ssuser7f90ae
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core ParcticalGaurav Mehta
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentSuresh Mohta
 

Similaire à Polymorphism presentation in java (20)

Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
Java Unit 2(part 3)
Java Unit 2(part 3)Java Unit 2(part 3)
Java Unit 2(part 3)
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
 
Java 06
Java 06Java 06
Java 06
 
Application package
Application packageApplication package
Application package
 
Oop in kotlin
Oop in kotlinOop in kotlin
Oop in kotlin
 
Abstraction in Java: Abstract class and Interfaces
Abstraction in  Java: Abstract class and InterfacesAbstraction in  Java: Abstract class and Interfaces
Abstraction in Java: Abstract class and Interfaces
 
Java notes
Java notesJava notes
Java notes
 
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
 
Lecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaionLecture4_Method_overloading power point presentaion
Lecture4_Method_overloading power point presentaion
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
Unit 4 notes.pdf
Unit 4 notes.pdfUnit 4 notes.pdf
Unit 4 notes.pdf
 
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
Diving in OOP (Day 4): Polymorphism and Inheritance (All About Abstract Class...
 
Inheritance
InheritanceInheritance
Inheritance
 
Java basics
Java basicsJava basics
Java basics
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
Java Core Parctical
Java Core ParcticalJava Core Parctical
Java Core Parctical
 
Basic concept of class, method , command line-argument
Basic concept of class, method , command line-argumentBasic concept of class, method , command line-argument
Basic concept of class, method , command line-argument
 

Dernier

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 

Dernier (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

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