1 / 86

Understanding Polymorphism

Understanding Polymorphism. public class Seed { public void grow() { System.out.println( “Seed grow”); } }. What is the output of program Test?. public class Apple extends Seed { public void grow() { System.out.println( “Apple grow” ); } }.

gafna
Download Presentation

Understanding Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Understanding Polymorphism

  2. public class Seed { public void grow() { System.out.println( “Seed grow”); } } What is the output of program Test? public class Apple extends Seed { public void grow() { System.out.println( “Apple grow” ); } } public class Test { public Test() { Seed x = new Apple(); x.grow(); } public static void main( String[] args ) { Test t = new Test(); } }

  3. OOP Basics In Java the simplest category of variables are the primitives. int x; x = 0; The primitive variables are not objects. Only one slot in memory is used to hold this variable. 1

  4. int x; x = 0; 1 2 3 4 5 6 x 0 7 8 9 10 11 12 • When we declare a primitive variable, it will occupy a single slot in memory. That slot will contain the value. 2

  5. OOP Basics Employee t; Now, we want to build an object. I have declared “t” to be a reference to an object of type Employee. At this point, the reference “t” does not point to any object. Soon, it will point to an object of type Employee, but now the object doesn’t exist. 3

  6. OOP Vocabulary Employee t = new Employee(); When this statement executes, the new keyword executes the default Constructor for Employee, which actually creates an object in memory and writes the location of that object in t. Now t points to the new Employee object. 4

  7. OOP Basics • It’s critical to understand the difference between a reference and the object to which it refers. Employee t; t = new Employee(); 5

  8. Employee t = new Employee(); t • When we declare a reference and instantiate the class, the location of the object is written in the reference. The reference points to the object. 6

  9. OOP Basics public class Employee { } • Now, let’s explore this object called employee. 7

  10. OOP Basics public class Employee { private String name; private double pay; } • I have added two instance variables: name and pay. 8

  11. OOP Basics public class Employee { private String name; private double pay; public Employee() { name = “”; pay = 0.0; } } • I have added a default constructor. 9

  12. Employee t = new Employee(); t name=“” pay=0.0 • This is what we have done. The object has instance variables, and methods we will use to operate on those instance variables. • If I want to make a concrete instance of this class, I need another class--a driver with a main method--to instantiate this class. 10

  13. OOP Basics • I have added getters and setters for the name. This is all pretty trivial so far. • The purpose for examining this class is to better understand its reference. public class Employee { private String name; private double pay; public Employee() { name = “”; pay = 0.0; } public String getName() { return name; } public void setName( String n ) { name = n; } } 11

  14. t Employee Employee() String getName() void setName( String) Up close and personal with this reference. What do we know from this reference? We know: • It can point to some object. • It expects that object to be of type Employee. • It knows that the object has these methods. 12

  15. OOP Basics • Here I have made a first stab at making this class do some work. I have given it the ‘calcPay()’ method. Unfortunately, this calcPay() method is only useful for an hourly employee. public class Employee { private double pay; private double rate; private int hours; public Employee() { pay = 0.0; rate = 0.0; hours = 0; } public double getPay() { return pay; } public double calcPay() { pay = rate * hours; } } 13

  16. OOP Basics public abstractclass Employee { private double pay; public Employee() { pay = 0.0; } public double getPay() { return pay; } public abstract double calcPay(); } • Now, I’ve changed the ‘calcPay()’ method. I’ve made it abstract. Also, the class itself becomes abstract. 14

  17. OOP Basics public class HourlyEmployee extends Employee { private double rate; private int hours; public HourlyEmployee( double r, int h ) { rate = r; hours = h; } public double calcPay() { return rate * hours; } } Superclass Subclass //Implicit call to Superclass constructor • What is the sequence of actions as this class is instantiated? 15

  18. public class Employee { public Employee() { System.out.println( “Employee constructor”); } } public class HourlyEmployee extends Employee { public HourlyEmployee() { System.out.println( “HourlyEmployee constructor”); } } public class TestEmployee { public TestEmployee() { HourlyEmployee he = new HourlyEmployee(); } public static void main( String[] args ) { TestEmployee te = new TestEmployee(); } } public class Object { public Object() { } } 16

  19. Superclasses and Subclasses • “is a” • Before the subclass HourlyEmployee object could be instantiated, we first had to instantiate the superclass Employee object. • A subclass is an instance of the superclass. It contains everything the superclass contains. • Because a subclass contains everything its superclass contains, we can say: “A subclass is an instance of its superclass.” 17

  20. Superclasses and Subclasses public class HourlyEmployee extends Employee { ... } • Whenever you extended another class, (a Superclass) you have created an instance of every class above your class in the class hierarchy. • When the Constructor for HourlyEmployee fires, it silently also fires the Constructors for Employee and every other class above it in the hierarchy. 18

  21. Superclasses and Subclasses • Every method, every data variable present in the Superclass is also present in the Subclass. • Make sure you understand this: If my class has everything above it—then I can say it is an example of the classes above it. 19

  22. The super Reference 20

  23. The super Reference • Already, we know that the Superclass-Subclass interaction has a big effect on the Constructor. • The Superclass Constructors are either implicitly or explicitly called first in the Constructor of a Subclass. super(whatever the Superclass needs) 21

  24. The super Reference super(whatever the Superclass needs) • Using this super reference, you can call the Constructor of your Subclass’ Direct Superclass. • If you use this super reference, then it must be the very first statement in the Subclass’ Constructor. 22

  25. public class HourlyEmployee extends Employee { double rate; int hours; public HourlyEmployee() { // implicit (hidden) call to Superclass Constructor. rate = 0.0; hours = 0; } public HourlyEmployee( double r, int h ) { // implicit (hidden) call to Superclass Constructor. rate = r; hours = h; } public double calc_pay() { return hours * rate; } } 23

  26. public class HourlyEmployee extends Employee { double rate; int hours; public HourlyEmployee() { super( “”, “” ); // Explicit call to Superclass Constructor rate = 0.0; hours = 0; } public HourlyEmployee( String n, String s, double r, int h ) { super( n, s ); // Explicit call to Superclass Constructor rate = r; hours = h; } public double calc_pay() { return hours * rate; } } 24

  27. The super Reference super(whatever the Superclass needs) • There is a similar statement that is used to call your Subclass’ constructor within your Subclass. • Don’t get the two confused. this(whatever the Subclass needs) 25

  28. Subclass can be Treated as an Object of the Superclass 26

  29. Superclasses and Subclasses • A Subclass contains more than its Superclass contains. • Because of that, we say inside of a Subclass is a complete copy of its Superclass. 27

  30. Superclasses and Subclasses • On previous slides, we said a Subclass is an object of the Superclass type. • Now, we take that one step further. 28

  31. Superclasses and Subclasses • A Subclass contains everything ( and more) required to make a complete example of the Superclass... • Thus, we say an object of a Subclass can be treated as an object of its Superclass. • In other words, in certain situations, we can ignore the difference between the Superclass and Subclass objects. 29

  32. Superclasses and Subclasses “An object of a Subclass can be treated as an object of its Superclass.” • What would stop this from being true? • Is there any part of the Superclass that is missing from the Subclass? Nope—it’s all there. 30

  33. Superclasses and Subclasses “An object of a Subclass can be treated as an object of its Superclass.” • Certainly if we are asking a Subclass to fill in for its parent, we throw away the extra stuff that the Subclass added, but—because a Subclass “is an” example of its Superclass—we can “treat the Subclass as an example of the Superclass.” 31

  34. Superclasses and Subclasses • And, if weonly want to call the methods from the Superclass and access the data variables that come from the Superclass—what’s the difference? 32

  35. Superclasses and Subclasses • Doing this might be interesting. • As long as we treat the reference like it only refers to the Superclass, we can handle a Subclass object from a Superclass “reference.” • As long as we only call the methods that exist in the Superclass, this will work fine. 33

  36. public class Employee { String name; String SSN; public double calc_pay() { return 0.0; } } public class SalariedEmployee extends Employee { double monthlySalary; public double calc_pay() { return monthlySalary; } } public class HourlyEmployee extends Employee { double hourly_rate; int hours; public double calc_pay() { return hours * hourly_rate; } } 34

  37. public class TestEmp { public static void main( String args[] ) { double money = 0.0; HourlyEmployee hour; hour = new HourlyEmployee(); money = hour.calc_pay(); System.out.println( “Money=” + money ); } } 35

  38. public class TestEmp { public static void main( String args[] ) { double money = 0.0; SalariedEmployeesalr; salr = new SalariedEmployee(); money = salr.calc_pay(); System.out.println( “Money=” + money ); } } 36

  39. public class TestEmp { public static void main( String args[] ) { double money = 0.0; Employeeempl; empl = new SalariedEmployee(); money = empl.calc_pay(); System.out.println( “Money=” + money ); } } • As long as we only call the methods that exist in the Superclass, this will work fine. 37

  40. public class TestEmp { public static void main( String args[] ) { double money = 0.0; Employeeempl; empl = new SalariedEmployee(); money = empl.calc_pay(); System.out.println( “Money=” + money ); } } • So, why in the world might we want to do this? 38

  41. public class RetiredEmployee extends Employee { double monthlyPension; public double calc_pay() { return monthlyPension; } } • Imagine if—after we built our system—we decided to add a class, for Retired Employees. • If we had built our program on a Superclass reference, then we wouldn’t have to rebuild anything. • The runtime environment sees what kind of object we have instantiated, and calls the right method override ! 39

  42. public class TestEmp { public static void main( String args[] ) { double money = 0.0; Employeeempl; empl = new RetiredEmployee(); money = empl.calc_pay(); System.out.println( “Money=” + money ); } } • Caution, this works only when you’re calling a method that exists in your Superclass, not one that exists only in the Subclass. (And so now you see the advantage of identifying a bunch of empty methods in your Superclass.) 40

  43. Superclasses and Subclasses • And (drumroll…) the whole process is called... Polymorphism 41

  44. Superclasses and Subclasses • More on Polymorphism • We can create a Superclass reference that is an array. • Then, when we instantiate the array, we can attach all different kinds of Subclass objects to the Superclass array reference. 42

  45. Superclasses and Subclasses • As long as we’re only calling methods that exist in the Superclass, we can work our way through the array and it will call all the correct overridden versions of each individual Subclass object. 43

  46. Superclasses and Subclasses • The Superclass reference only knows about the methods that exist in the Superclass. • The Superclass only tries to call the methods it knows about. • That’s perfectly fine, because all the methods in the Superclass are available in its Subclass. 44

  47. Superclasses and Subclasses • Therefore the Superclass isn’t aware that the object it references can do a whole lot more than the Superclass thinks it can. 45

  48. Superclasses and Subclasses • So, “We can create a Superclass reference array that actually points to a Subclass object.” • As long as we treat those Subclass objects as if they were Superclass objects, (meaning we only call the methods in the Superclass) we have no problems. 46

  49. Superclasses and Subclasses • The Subclass knows how to do everything its parent Superclass can do. • The kids can do everything the parent can. 47

  50. Superclasses and Subclasses • However, if we try to do it the other way around—treating the Superclass as if it were one of its children—then we can have... problems • Why? The Subclass can do many things the Superclass cannot. • The kids can do many things the parent cannot. 48

More Related