Chapter 8 Polymorphism

1,566 views
1,491 views

Published on

Chapter 8 Polymorphism
Taught by Oum Saokosal, Head of Information Technology, National Polytechnic Institute of Cambodia

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

No Downloads
Views
Total views
1,566
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
149
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Chapter 8 Polymorphism

  1. 1. Chapter 8 Inheritance and Polymorphism Oum Saokosal , Head of IT Department National Polytechnic Institute of Cambodia Tel: (855)-12-417214 E-mail: oum_saokosal@yahoo.com
  2. 2. Chapter 8 Polymorphism
  3. 3. Polymorphism <ul><li>What is polymorphism? </li></ul><ul><li>Why polymorphism? </li></ul><ul><li>Specific uses of polymorphism </li></ul><ul><li>Notes </li></ul><ul><li>Remember </li></ul><ul><li>Casting </li></ul><ul><li>instanceof Operations </li></ul><ul><li>Remember </li></ul>
  4. 4. What is Polymorphism? (1) <ul><li>OOP has 3 features: </li></ul><ul><ul><li>Class encapsulation </li></ul></ul><ul><ul><li>Inheritance </li></ul></ul><ul><ul><li>Polymorphism </li></ul></ul><ul><li>Polymorphism is a mechanism to allow a single variable to refer to objects from different classes. </li></ul>
  5. 5. What is Polymorphism? Example (2) <ul><li>Assume we have GraduateStudent which inherits from Student . Here is an example. </li></ul><ul><ul><li>public class Student{ </li></ul></ul><ul><ul><li>public String getClassName() { </li></ul></ul><ul><ul><li>return &quot;Student Class&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>Student +getClassName():String UndergraduateStudent +getClassName():String GraduateStudent +getClassName():String
  6. 6. What is Polymorphism? Example (3) <ul><li>//Continue ... </li></ul><ul><ul><li>public class GraduateStudent extends Student { </li></ul></ul><ul><ul><li>@Override public String getClassName() { </li></ul></ul><ul><ul><li>return &quot;GraduateStudent Class&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public class UndergraduateStudent extends Student { </li></ul></ul><ul><ul><li>@Override public String getClassName() { </li></ul></ul><ul><ul><li>return “UndergraduateStudent Class&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  7. 7. What is Polymorphism? Test 1 (4) <ul><li>//Test1 1 – Polymorphism </li></ul><ul><ul><li>public class TestPoly1{ </li></ul></ul><ul><ul><li>public static void main(String[] args){ </li></ul></ul><ul><ul><li>Student s = new UndergraduateStudent(); </li></ul></ul><ul><ul><li>System.out.println( s.getClassName() ); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Q. What is output? </li></ul><ul><li>A. UnderGraduateStudent Class </li></ul>That’s polymorphism
  8. 8. What is Polymorphism? Test 2 (5) <ul><li>//Example 2 – Without Polymorphism </li></ul><ul><ul><li>public class TestNoPoly2 { </li></ul></ul><ul><ul><li>public static void main(String[] args){ </li></ul></ul><ul><ul><li> GraduateStudent gs = new GraduateStudent(); </li></ul></ul><ul><ul><li>displayUnder ( gs ); </li></ul></ul><ul><ul><li>displayGraduate ( new UndergraduateStudent() ); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>static void displayUnder ( GraduateStudent gs ){ </li></ul></ul><ul><ul><li>System.out.println(gs.getClassName()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>static void displayGraduate ( UndergraduateStudent us ){ </li></ul></ul><ul><ul><li>System.out.println(us.getClassName()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  9. 9. What is Polymorphism? Test 2 (6) <ul><li>//Example 2 – With Polymorphism </li></ul><ul><ul><li>public class TestPoly2 { </li></ul></ul><ul><ul><li>public static void main(String[] args){ </li></ul></ul><ul><ul><li>display( new GraduateStudent() ); </li></ul></ul><ul><ul><li>display( new UndergraduateStudent() ); </li></ul></ul><ul><ul><li> } </li></ul></ul><ul><ul><li>//Polymorphic method </li></ul></ul><ul><ul><li> public static void display( Student stu ){ </li></ul></ul><ul><ul><li>System.out.println( stu.getClassName() ); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  10. 10. <ul><li>Why Polymorphism? </li></ul><ul><li>To choose which method from which class is called at runtime dynamically. </li></ul><ul><li>A variable of a parent type can refer to any child and grand child of the parent. </li></ul><ul><li>Student stu = new GraduateStudent(); </li></ul><ul><li>stu.getClassName(); </li></ul>Why Polymorphism? (1) Parent/Superclass Children/Subclass Student getClassName():String UndergraduateStudent getClassName():String GraduateStudent getClassName():String
  11. 11. Specific Uses of Polymorphism Methods (1) <ul><li>Methods </li></ul><ul><li>If we don’t use polymorphism, you have to make a new method for every different parameter data type. Look at slide 8 . </li></ul><ul><li>But with polymorphism, you make only one method for every parameter which has the same parent. Look at slide 9 . </li></ul>
  12. 12. Specific Uses of Polymorphism Array (2) <ul><li>Array: In Java, every element in an array has the same type, doesn’t it? E.g. </li></ul><ul><li>double[] scores = new double[10]; </li></ul><ul><li>scores[0] = 1.4; </li></ul><ul><li>scores[1] = “Hi”; //Compile Error </li></ul><ul><li>Circle[] circles = new Circle[5]; </li></ul><ul><li>circles[0] = new Circle(5); </li></ul><ul><li>circles[1] = new Circle(20); </li></ul><ul><li>circles[2] = new Student(); //Compile Error </li></ul>
  13. 13. Specific Uses of Polymorphism Array (3) <ul><li>Q. If I do want to store different types of objects in one array, how can I do it? </li></ul><ul><li>Use polymorphism . </li></ul><ul><li>If the objects are inherited from the same parent , we can store them in the same array . </li></ul><ul><li>Example: </li></ul><ul><ul><li>Student[] students = new Student[3]; </li></ul></ul><ul><ul><li>students[0] = new Student(); </li></ul></ul><ul><ul><li>students[1] = new GraduateStudent(); </li></ul></ul><ul><ul><li>students[2] = new UndergraduateStudent(); </li></ul></ul>
  14. 14. Specific usages of Polymorphism Array (4) <ul><ul><li>public class TestPoly3 { </li></ul></ul><ul><ul><li>public static void main(String[] args){ </li></ul></ul><ul><ul><li>Student[] s = new Student[3]; </li></ul></ul><ul><ul><li>s[0] = new Student(); </li></ul></ul><ul><ul><li>s[1] = new GraduateStudent(); </li></ul></ul><ul><ul><li>s[2] = new UndergraduateStudent(); </li></ul></ul><ul><ul><li>display(s); //Polymorphism </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public static void display( Student [] s){ </li></ul></ul><ul><ul><li>for(int i = 0; i<s.length; i++){ </li></ul></ul><ul><ul><li>System.out.println(s[i].getClassName()); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  15. 15. Note #1 (1) <ul><li>Look at the codes again. </li></ul><ul><ul><li>public class Student{ </li></ul></ul><ul><ul><li>public String getClassName(){ </li></ul></ul><ul><ul><li>return &quot;Student Class&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public class GraduateStudent extends Student{ </li></ul></ul><ul><ul><li>@Override public String getClassName(){ </li></ul></ul><ul><ul><li>return &quot;GraduateStudent Class&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public class UndergraduateStudent extends Student{ </li></ul></ul><ul><ul><li> @Override public String getClassName(){ </li></ul></ul><ul><ul><li>return “UndergraduateStudent Class&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>GraduateStudent and UndergraduateStudent override getClassName() method!
  16. 16. Note #1 (2) <ul><li>Please note that the two subclasses of Student override getClassName() . </li></ul><ul><li>Then, we can use polymorphism to refer to getClassName() in the sub class. </li></ul><ul><ul><li>public class TestPoly1{ </li></ul></ul><ul><ul><li>public static void main(String[] args){ </li></ul></ul><ul><ul><li>Student s; </li></ul></ul><ul><ul><li>s = new GraduateStudent(); </li></ul></ul><ul><ul><li>System.out.println( s.getClassName() ); </li></ul></ul><ul><ul><li>s = new UndergraduateStudent(); </li></ul></ul><ul><ul><li>System.out.println( s.getClassName() ); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  17. 17. Note #2 (1) <ul><li>But now in a subclass GraduateStudent , we add a new method, say getDegree() . </li></ul><ul><ul><li>public class GraduateStudent extends Student{ </li></ul></ul><ul><ul><li>public String getClassName(){ </li></ul></ul><ul><ul><li>return &quot;GraduateStudent Class&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li> public String getDegree(){ </li></ul></ul><ul><ul><li>return &quot;Msc in Computer Science&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  18. 18. Note #2 (2) <ul><li>And then we want to use polymorphism to call getDegree() in the subclass, </li></ul><ul><ul><li>public class TestPoly1{ </li></ul></ul><ul><ul><li>public static void main(String[] args){ </li></ul></ul><ul><ul><li>Student s = new GraduateStudent(); </li></ul></ul><ul><ul><li>System.out.println( s.getDegree() ); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>It will show a compile error because Student class doesn’t have getDegree() . </li></ul>
  19. 19. Note #3 (1) <ul><li>What about if we want add a method, say getID() to the parent, Student . </li></ul><ul><li>public class Student{ </li></ul><ul><ul><li>public String getClassName(){ </li></ul></ul><ul><ul><li>return &quot;Student Class&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li> public String getID(){ </li></ul></ul><ul><ul><li>return “NPIC001&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul>
  20. 20. Note #3 (2) <ul><li>Then we want to call getID(). </li></ul><ul><ul><li>public class TestPoly1{ </li></ul></ul><ul><ul><li>public static void main(String[] args){ </li></ul></ul><ul><ul><li>Student s = new GraduateStudent(); </li></ul></ul><ul><ul><li>System.out.println( s.getID() ); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Q. Is it a compile error? </li></ul><ul><li>A. No . As long as getID() is in Student , we can call it without any question . </li></ul>
  21. 21. The bottom line (1) <ul><li>As long as subclasses inherit from the same parent , you can declare a variable as the parent type and then refer to any subclasses . </li></ul><ul><li>Student stu; //stu is parent type </li></ul><ul><li>//stu refers to subclasses </li></ul><ul><li>stu = new UndergraduateStudent(); </li></ul><ul><li>stu = new GraduateStudent(); </li></ul>Student getClassName():String UndergraduateStudent getClassName():String GraduateStudent getClassName():String
  22. 22. The bottom line (2) <ul><li>When you call a method which exists both in a parent and subclass, then it will dynamically refer to the method in the subclass. (This is the main reason to use Polymorphism) See slide 7 . </li></ul><ul><li>You cannot call methods that are NOT existed in parent. If you try it, you will get compile error. See slide 18 . </li></ul><ul><li>However, you can call every method in the superclass. See slide 19 & 20 . </li></ul>
  23. 23. Remember (1) <ul><li>Polymorphism is a mechanism to enable us to choose a method at runtime dynamically. </li></ul><ul><li>Polymorphism always needs Inheritance . </li></ul><ul><li>Polymorphism always needs Overridden Methods . </li></ul><ul><li>There is no keyword to indicate Polymorphism. </li></ul><ul><li>You can recognise it whenever you see a class type that is different from its constructor . E.g. </li></ul><ul><ul><li>Student s = new GraduateStudent() ; </li></ul></ul><ul><li>You can also recognise it, when a class type is Object class. E.g. </li></ul><ul><ul><li>private void aMethod( Object ob){...} </li></ul></ul>
  24. 24. Remember (2) <ul><li>Polymorphism is not as vital as inheritance. That is, without polymorphism, you still CAN make a Java program; however without inheritance, especially when using Java library code, you CANNOT achieve very much. </li></ul>
  25. 25. Casting Objects (1) <ul><li>What is Casting? </li></ul><ul><li>Casting is a technique to convert a variable from one primitive type to another one. E.g.: </li></ul><ul><li>double d1 = 2.5; </li></ul><ul><li>double d2 = 12; </li></ul><ul><li>int i1 = 23; </li></ul><ul><li>int i2 = 22.5 ; // Compile Error </li></ul><ul><li>How to solve this error? </li></ul><ul><li>int i2 = (int) 22.5; // i2 = 22 </li></ul><ul><li>//Casting from double 22.5 to integer. </li></ul>
  26. 26. Casting Objects (2) <ul><li>To understand casting, let’s meet a group of animals studying polymorphism. </li></ul>CB is a curious student who always asks questions whereas SR is a smart student. She can help to answer CB’s questions. SR : Smart Rabbit CB: Curious Ball
  27. 27. Casting Objects (3) <ul><li>CB : Hey Smart Rabbit! Can I ask you some questions? </li></ul><ul><li>SR: Yes, Of course. What can I do for ya? </li></ul><ul><li>CB: I have 2 classes, Grape inherits from Fruit . </li></ul>
  28. 28. Casting Objects (4) <ul><li>SR: So what? </li></ul><ul><li>CB: Grape has four methods. And Fruit has only two methods. Then I want to use polymorphism like this: </li></ul><ul><li>Fruit f = new Grape(); </li></ul><ul><li>So which methods can I call from f ? </li></ul><ul><ul><li>f ._ _ _ ? </li></ul></ul><ul><li>SR: Then you can call only those methods from Fruit class like isEatable() and getColor() , and, of course, other 9 methods from Object class. </li></ul>
  29. 29. Casting Objects (5) <ul><li>CB: You know, usually when I define a variable with a parent type and refer to a constructor from the same class, I can call every method from it. </li></ul><ul><li>Grape g = new Grape(); </li></ul><ul><li>g.getTaste(); // OK </li></ul><ul><li>g.getVitamin(); //OK </li></ul><ul><li>SR: Yeh, and what is your problem? </li></ul><ul><li>CB: But now when I use polymorphism, it seems to me that I couldn’t do what I used to do. </li></ul><ul><ul><li>Fruit fruit = new Grape(); </li></ul></ul><ul><ul><li>fruit .getTaste() ; // Compile Error </li></ul></ul><ul><ul><li>fruit .getVitamin() ; // Compile Error </li></ul></ul>
  30. 30. Casting Objects (6) <ul><li>CB: So do you have any solution, Rabbit? </li></ul><ul><li>SR: Why not? You can use “casting” to cast from Fruit to another Grape variable. </li></ul><ul><li>CB: Can you show me? </li></ul><ul><li>SR: Sure! Here it is: </li></ul><ul><ul><li>Fruit fruit = new Grape(); </li></ul></ul><ul><ul><li>Grape grape = (Grape) fruit; // Casting </li></ul></ul><ul><ul><li>grape .getTaste() ; //OK </li></ul></ul><ul><ul><li>grape .getVitamin() ; //OK </li></ul></ul>
  31. 31. Casting Objects (6) <ul><li>CB: Yeh... but you still create a variable that has Grape as its parent type. It doesn’t make sense to me? </li></ul><ul><li>SR: Why not? </li></ul><ul><li>CB: You know, it is much better if I just create a variable which has Grape as its type and refer to its constructor since the beginning like this: </li></ul><ul><li>Grape g = new Grape() ; </li></ul><ul><li>g.getTaste(); // OK </li></ul><ul><li>g.getVitamin(); //OK </li></ul>
  32. 32. Casting Objects (7) <ul><li>SR: Well, but do you remember polymorphic method? </li></ul><ul><li>CB: Yes. </li></ul><ul><li>SR: So can I tell me some about it? </li></ul><ul><li>CB: polymorphic method is just like other methods but the type of parameters is always a superclass. For example: </li></ul><ul><li>public static void display( Object o){ </li></ul><ul><li>JOptionPane.showMessageDialog(null, o); </li></ul><ul><li>} </li></ul><ul><li>SR: Yehh! You are good! </li></ul>
  33. 33. Casting Objects (8) <ul><li>CB: Thank you. But how polymorphic method works with casting? </li></ul><ul><li>SR: OK! I’ll show you the differences when not using casting and when using casting. </li></ul><ul><li>public class TestNoCasting { </li></ul><ul><li>public static void main(String[] args){ </li></ul><ul><li>showTaste(new Grape()); </li></ul><ul><li>} </li></ul><ul><li>public static void showTaste(Fruit fruit){ </li></ul><ul><li>JOptionPane.showMessageDialog(null, </li></ul><ul><li>fruit.getTaste() ); </li></ul><ul><li>} //Error because Fruit has not getTaste() </li></ul><ul><li>} </li></ul>
  34. 34. Casting Objects (9) <ul><li>SR: Here is codes when using casting. </li></ul><ul><li>public class TestCastingObject { </li></ul><ul><li>public static void main(String[] args){ </li></ul><ul><li>showTaste(new Grape()); </li></ul><ul><li>} </li></ul><ul><li>public static void showTaste(Fruit fruit){ </li></ul><ul><li>Grape grape = (Grape) fruit; </li></ul><ul><li>JOptionPane.showMessageDialog(null, </li></ul><ul><li>grape.getTaste() ); //OK </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  35. 35. Casting Objects (10) <ul><li>CB: Wow! Now I got. Casting can help polymorphism more powerful. Both Java and you are very smart. </li></ul><ul><li>SR: Not yet dude! </li></ul><ul><li>CB: What else? </li></ul><ul><li>SR: What about if I add another class, say Apple inherits also from Fruit ? Let’s see it on the next slide. </li></ul>
  36. 36. Casting Objects (11)
  37. 37. Casting Objects (11) <ul><li>SR: Then I pass in Apple object to showTaste() method. </li></ul><ul><li>public class TestCastingObject { </li></ul><ul><li>public static void main(String[] args){ </li></ul><ul><li>showTaste( new Apple() ); </li></ul><ul><li>} </li></ul><ul><li>public static void showTaste(Fruit fruit){ </li></ul><ul><li>Grape grape = (Grape) fruit; </li></ul><ul><li>JOptionPane.showMessageDialog(null, </li></ul><ul><li>grape.getTaste()); </li></ul><ul><li>} </li></ul><ul><li>} //Runtime Error: casting wrong class </li></ul>
  38. 38. instanceof Operation (1) <ul><li>CB: Wo.. ho.. ho. I didn’t realize it. </li></ul><ul><li>SR: Don’t worry! Remember Java is number one in the world. </li></ul><ul><li>CB: So what is your solution? </li></ul><ul><li>SR: We can use instanceof to test whether it is correct object or not before we cast it. </li></ul><ul><li>CB: Can you show how? </li></ul><ul><li>SR: OK, Let’s go to the next slide. </li></ul>
  39. 39. instanceof Operation (2) <ul><li>public static void main(String[] args) { </li></ul><ul><li>showTaste(new Apple()); </li></ul><ul><li>} </li></ul><ul><li>public static void showTaste(Fruit fruit) { </li></ul><ul><li>if (fruit instanceof Grape ) { </li></ul><ul><li>Grape grape = (Grape) fruit; System.out.print(grape.getTaste()); </li></ul><ul><li>} else if(fruit instanceof Apple ) { </li></ul><ul><li>Apple apple = (Apple) fruit; System.out.print(apple.getTaste()); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  40. 40. instanceof Operation (3) CB: OK. It seems to me that I know them all, but I want to ask you, Smart Rabbit, whether the 3rd year students in NPIC who are watching us understand it or not. SR: This is a hard question. I’m not sure either. You’ve better ask them by yourself.
  41. 41. Remember (1) <ul><li>Casting is a technique to convert a variable from one class type to another one within an inheritance hierarchy . </li></ul><ul><li>That is, if we cast a class to another class in different root, you will get run-time error . E.g: </li></ul><ul><li>Fruit fruit = new Grape(); </li></ul><ul><li>Grape grape = (Grape) fruit ; // OK </li></ul><ul><li>Apple apple = (Apple) fruit ; //Runtime Error </li></ul>
  42. 42. Remember (2) <ul><li>A keyword instanceof is an operation to check whether a class that we want to cast is in the correct hierarchy or not. </li></ul><ul><li>E.g.: </li></ul><ul><ul><li>Fruit fruit = new Grape(); </li></ul></ul><ul><ul><li>if(fruit instanceof Grape){ </li></ul></ul><ul><ul><li>Grape grape = (Grape) fruit ; </li></ul></ul><ul><ul><li>} else if(fruit instanceof Apple){ </li></ul></ul><ul><ul><li>Apple apple = (Apple) fruit ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>fruit.getColor(); </li></ul></ul><ul><ul><li>fruit.getTaste(); </li></ul></ul>

×