SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
Polymorphism


               1
Agenda

    What is and Why Polymorphism?
●


    Examples of Polymorphism in Java programs
●


    3 forms of Polymorphism
●




                                                2
What is & Why
Polymorphism?
                 3
What is Polymorphism?

    Generally, polymorphism refers to the ability to
●

    appear in many forms
    Polymorphism in a Java program
●


        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 super class,
        while automatically selecting the proper methods to
        apply to a particular object based on the subclass it
        belongs to

                                                                4
Polymorphism Example

    For example, given a base class shape,
●

    polymorphism enables the programmer to define
    different area methods for any number of derived
    classes, such as circles, rectangles and triangles.
    No matter what shape an object is, applying the
●

    area method to it will return the correct results.




                                                          5
Examples of
Polymorphic Behavior
  in Java Programs
                       6
Example #1: Polymorphism
    Given the parent class Person and the child class
●

    Student, we add another subclass of Person
    which is Employee.
    Below is the class hierarchy
●




                                                        7
Example #1: Polymorphism
    In Java, we can create a reference that is of type
●

    super class, Person, to an object of its subclass,
    Student.
    public static main( String[] args ) {

        Student studentObject = new Student();
        Employee employeeObject = new Employee();

        Person ref = studentObject; // Person reference points
                                    // to a Student object

        // Calling getName() of the Student object instance
        String name = ref.getName();
    }

                                                              8
Example #1: Polymorphism
    Now suppose we have a getName method in our
●

    super class Person, and we override this method in
    both Student and Employee subclass's
    public class Student {
       public String getName(){
          System.out.println(“Student Name:” + name);
          return name;
       }
    }

    public class Employee {
       public String getName(){
          System.out.println(“Employee Name:” + name);
          return name;
       }
                                                         9
    }
Example #1: Polymorphism

    Going back to our main method, when we try to call
●

    the getName method of the reference Person ref,
    the getName method of the Student object will be
    called.

    Now, if we assign ref to an Employee object, the
●

    getName method of Employee will be called.




                                                         10
Example #1: Polymorphism
1 public static main( String[] args ) {
2
3    Student studentObject = new Student();
4    Employee employeeObject = new Employee();
5
6    Person ref = studentObject; //Person ref. points to a
7                            // Student object
8
9    // getName() method of Student class is called
10   String temp= ref.getName();
11   System.out.println( temp );
12
13   ref = employeeObject; //Person ref. points to an
14                           // Employee object
15
16   //getName() method of Employee class is called
17   String temp = ref.getName();
18   System.out.println( temp );
19 }

                                                             11
Example #2: Polymorphism

    Another example that illustrates polymorphism is
●

    when we try to pass a reference to methods as a
    parameter

    Suppose we have a static method printInformation
●

    that takes in a Person reference as parameter.
    public static printInformation( Person p ){
        // It will call getName() method of the
        // actual object instance that is passed
        p.getName();
    }


                                                       12
Example #2: Polymorphism

    We can actually pass a reference of type Employee
●

    and type Student to the printInformation method as
    long as it is a subclass of the Person class.

public static main( String[] args ){

       Student    studentObject = new Student();
       Employee   employeeObject = new Employee();

       printInformation( studentObject );

       printInformation( employeeObject );
}



                                                         13
Benefits of
Polymorphism
                14
Benefits of Polymorphism

    Simplicity
●


        If you need to write code that deals with a family of types,
    –
        the code can ignore type-specific details and just interact
        with the base type of the family
        Even though the code thinks it is using an object of the
    –
        base class, the object's class could actually be the base
        class or any one of its subclasses
        This makes your code easier for you to write and easier
    –
        for others to understand




                                                                       15
Benefits of Polymorphism

    Extensibility
●


        Other subclasses could be added later to the family of
    –
        types, and objects of those new subclasses would also
        work with the existing code




                                                                 16
3 Forms of
Polymorphism
               17
3 Forms of Polymorphism
             in Java program
    Method overriding
●


        Methods of a subclass override the methods of a
    –
        superclass
    Method overriding (implementation) of the abstract
●

    methods
        Methods of a subclass implement the abstract methods
    –
        of an abstract class
    Method overriding (implementation) through the
●

    Java interface
        Methods of a concrete class implement the methods of
    –
        the interface
                                                               18
Polymorphism


               19

Contenu connexe

Tendances

Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)MD Sulaiman
 
Object-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism UnleashedObject-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism UnleashedNaresh Chintalcheru
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingAshita Agrawal
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming languageMd.Al-imran Roton
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In JavaSpotle.ai
 
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
 
Polymorphism in Java
Polymorphism in JavaPolymorphism in Java
Polymorphism in JavaJava2Blog
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdmHarshal Misalkar
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphismlalithambiga kamaraj
 
Java Inheritance
Java InheritanceJava Inheritance
Java InheritanceVINOTH R
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 

Tendances (20)

Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
 
Object-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism UnleashedObject-Oriented Polymorphism Unleashed
Object-Oriented Polymorphism Unleashed
 
inheritance c++
inheritance c++inheritance c++
inheritance c++
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Polymorphism In Java
Polymorphism In JavaPolymorphism In Java
Polymorphism In Java
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
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
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Polymorphism in Java
Polymorphism in JavaPolymorphism in Java
Polymorphism in Java
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 

En vedette

En vedette (6)

Inheritance
InheritanceInheritance
Inheritance
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Co crystalization
Co crystalizationCo crystalization
Co crystalization
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Similaire à Polymorphism

Chapter8:Understanding Polymorphism
Chapter8:Understanding PolymorphismChapter8:Understanding Polymorphism
Chapter8:Understanding PolymorphismIt Academy
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingIqra khalil
 
Object Oriented programming Using Python.pdf
Object Oriented programming Using Python.pdfObject Oriented programming Using Python.pdf
Object Oriented programming Using Python.pdfRishuRaj953240
 
9-_Object_Oriented_Programming_Using_Python.pdf
9-_Object_Oriented_Programming_Using_Python.pdf9-_Object_Oriented_Programming_Using_Python.pdf
9-_Object_Oriented_Programming_Using_Python.pdfanaveenkumar4
 
P.7 media 2 polymorphism
P.7 media 2 polymorphismP.7 media 2 polymorphism
P.7 media 2 polymorphismahmadmuzaqqi
 
Principles of Object Oriented Programming
Principles of Object Oriented ProgrammingPrinciples of Object Oriented Programming
Principles of Object Oriented ProgrammingKasun Ranga Wijeweera
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questionsnishajj
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Learn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdfLearn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdfDatacademy.ai
 
PythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programmingPythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programmingKhadijaKhadijaAouadi
 
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...ShuvrojitMajumder
 

Similaire à Polymorphism (20)

Chapter8:Understanding Polymorphism
Chapter8:Understanding PolymorphismChapter8:Understanding Polymorphism
Chapter8:Understanding Polymorphism
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Java
JavaJava
Java
 
Object Oriented programming Using Python.pdf
Object Oriented programming Using Python.pdfObject Oriented programming Using Python.pdf
Object Oriented programming Using Python.pdf
 
9-_Object_Oriented_Programming_Using_Python.pdf
9-_Object_Oriented_Programming_Using_Python.pdf9-_Object_Oriented_Programming_Using_Python.pdf
9-_Object_Oriented_Programming_Using_Python.pdf
 
P.7 media 2 polymorphism
P.7 media 2 polymorphismP.7 media 2 polymorphism
P.7 media 2 polymorphism
 
Lecture 17
Lecture 17Lecture 17
Lecture 17
 
Principles of Object Oriented Programming
Principles of Object Oriented ProgrammingPrinciples of Object Oriented Programming
Principles of Object Oriented Programming
 
8 polymorphism
8 polymorphism8 polymorphism
8 polymorphism
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Learn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdfLearn Polymorphism in Python with Examples.pdf
Learn Polymorphism in Python with Examples.pdf
 
PythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programmingPythonOO.pdf oo Object Oriented programming
PythonOO.pdf oo Object Oriented programming
 
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...Shuvrojit Majumder .  25900120006  Object Oriented Programming (PCC-CS 503)  ...
Shuvrojit Majumder . 25900120006 Object Oriented Programming (PCC-CS 503) ...
 

Dernier

Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 

Dernier (20)

Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 

Polymorphism

  • 2. Agenda What is and Why Polymorphism? ● Examples of Polymorphism in Java programs ● 3 forms of Polymorphism ● 2
  • 3. What is & Why Polymorphism? 3
  • 4. What is Polymorphism? Generally, polymorphism refers to the ability to ● appear in many forms Polymorphism in a Java program ● 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 super class, while automatically selecting the proper methods to apply to a particular object based on the subclass it belongs to 4
  • 5. Polymorphism Example For example, given a base class shape, ● polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the ● area method to it will return the correct results. 5
  • 6. Examples of Polymorphic Behavior in Java Programs 6
  • 7. Example #1: Polymorphism Given the parent class Person and the child class ● Student, we add another subclass of Person which is Employee. Below is the class hierarchy ● 7
  • 8. Example #1: Polymorphism In Java, we can create a reference that is of type ● super class, Person, to an object of its subclass, Student. public static main( String[] args ) { Student studentObject = new Student(); Employee employeeObject = new Employee(); Person ref = studentObject; // Person reference points // to a Student object // Calling getName() of the Student object instance String name = ref.getName(); } 8
  • 9. Example #1: Polymorphism Now suppose we have a getName method in our ● super class Person, and we override this method in both Student and Employee subclass's public class Student { public String getName(){ System.out.println(“Student Name:” + name); return name; } } public class Employee { public String getName(){ System.out.println(“Employee Name:” + name); return name; } 9 }
  • 10. Example #1: Polymorphism Going back to our main method, when we try to call ● the getName method of the reference Person ref, the getName method of the Student object will be called. Now, if we assign ref to an Employee object, the ● getName method of Employee will be called. 10
  • 11. Example #1: Polymorphism 1 public static main( String[] args ) { 2 3 Student studentObject = new Student(); 4 Employee employeeObject = new Employee(); 5 6 Person ref = studentObject; //Person ref. points to a 7 // Student object 8 9 // getName() method of Student class is called 10 String temp= ref.getName(); 11 System.out.println( temp ); 12 13 ref = employeeObject; //Person ref. points to an 14 // Employee object 15 16 //getName() method of Employee class is called 17 String temp = ref.getName(); 18 System.out.println( temp ); 19 } 11
  • 12. Example #2: Polymorphism Another example that illustrates polymorphism is ● when we try to pass a reference to methods as a parameter Suppose we have a static method printInformation ● that takes in a Person reference as parameter. public static printInformation( Person p ){ // It will call getName() method of the // actual object instance that is passed p.getName(); } 12
  • 13. Example #2: Polymorphism We can actually pass a reference of type Employee ● and type Student to the printInformation method as long as it is a subclass of the Person class. public static main( String[] args ){ Student studentObject = new Student(); Employee employeeObject = new Employee(); printInformation( studentObject ); printInformation( employeeObject ); } 13
  • 15. Benefits of Polymorphism Simplicity ● If you need to write code that deals with a family of types, – the code can ignore type-specific details and just interact with the base type of the family Even though the code thinks it is using an object of the – base class, the object's class could actually be the base class or any one of its subclasses This makes your code easier for you to write and easier – for others to understand 15
  • 16. Benefits of Polymorphism Extensibility ● Other subclasses could be added later to the family of – types, and objects of those new subclasses would also work with the existing code 16
  • 18. 3 Forms of Polymorphism in Java program Method overriding ● Methods of a subclass override the methods of a – superclass Method overriding (implementation) of the abstract ● methods Methods of a subclass implement the abstract methods – of an abstract class Method overriding (implementation) through the ● Java interface Methods of a concrete class implement the methods of – the interface 18