Understanding Polymorphism

1
Polymorphism
• Exam Objective 1.5 Describe polymorphism as it applies
to classes and interfaces, and describe and apply th...
Practical Examples of Polymorphism
• Exam Objective 3.4 Develop code that uses
Polymorphism for both classes and interface...
What is & Why
Polymorphism?
4
What is Polymorphism?





Generally, polymorphism refers to the ability to appear
in many forms
Polymorphism in a Java ...
Polymorphism Example



For example, given a base class shape, polymorphism
enables the programmer to define different ar...
Examples of
Polymorphic Behavior
in Java Programs
7
Example #1: Polymorphism




Given the parent class Person and the child class
Student, we add another subclass of Perso...
Example #1: Polymorphism


In Java, we can create a reference that is of type super
class, Person, to an object of its su...
Example #1: Polymorphism


Now suppose we have a getName method in our super
class Person, and we override this method in...
Example #1: Polymorphism





Going back to our main method, when we try to call the
getName method of the reference Per...
Example #1: Polymorphism
1

public static main( String[] args ) {

2
3

Student studentObject = new Student();
Employee em...
Example #2: Polymorphism





Another example that illustrates polymorphism is when
we try to pass a reference to method...
Example #2: Polymorphism



We can actually pass a reference of type Employee
and type Student to the printInformation me...
Benefits of
Polymorphism
15
Benefits of Polymorphism



Simplicity
If you need to write code that deals with a family of subtypes, the code can ignor...
Benefits of Polymorphism



Extensibility
Other subclasses could be added later to the family of types,
and objects of th...
3 Forms of
Polymorphism
18
3 Forms of Polymorphism
in Java program


Method overriding
Methods of a subclass override the methods of a superclass

...
20
21
22
23
24
25
26
27
28
29
30
31
Upcoming SlideShare
Loading in...5
×

Chapter8:Understanding Polymorphism

271

Published on

Exam Objective 1.5 Describe polymorphism as it applies to classes and interfaces, and describe and apply the "program to an interface" principle

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
271
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
20
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Chapter8:Understanding Polymorphism

  1. 1. Understanding Polymorphism 1
  2. 2. Polymorphism • Exam Objective 1.5 Describe polymorphism as it applies to classes and interfaces, and describe and apply the "program to an interface" principle 2
  3. 3. Practical Examples of Polymorphism • Exam Objective 3.4 Develop code that uses Polymorphism for both classes and interfaces, and recognize code that uses the "program to an interface" principle. 3
  4. 4. What is & Why Polymorphism? 4
  5. 5. 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 5
  6. 6. 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. The area method of circle, rectangle, and triangle is implemented differently  No matter what shape an object is, applying the area method to it will return the correct results. 6
  7. 7. Examples of Polymorphic Behavior in Java Programs 7
  8. 8. 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 8
  9. 9. 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(); } 9
  10. 10. 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; } } 10
  11. 11. 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. 11
  12. 12. Example #1: Polymorphism 1 public static main( String[] args ) { 2 3 Student studentObject = new Student(); Employee employeeObject = new Employee(); 4 5 Person ref = studentObject; //Person ref. points to a 6 7 8 // getName() method of Student class is called String temp= ref.getName(); System.out.println( temp ); 9 10 11 12 13 14 15 ref = employeeObject; //Person ref. points to an // Student object // Employee object //getName() method of Employee class is called String temp = ref.getName(); System.out.println( temp ); } 12
  13. 13. 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(); } 13
  14. 14. 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 ); } 14
  15. 15. Benefits of Polymorphism 15
  16. 16. Benefits of Polymorphism  Simplicity If you need to write code that deals with a family of subtypes, 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 16
  17. 17. 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 17
  18. 18. 3 Forms of Polymorphism 18
  19. 19. 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 19
  20. 20. 20
  21. 21. 21
  22. 22. 22
  23. 23. 23
  24. 24. 24
  25. 25. 25
  26. 26. 26
  27. 27. 27
  28. 28. 28
  29. 29. 29
  30. 30. 30
  31. 31. 31
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×