




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





The basic idea behind polymorphism is that the decision of which method is called can sometimes be delayed till run-time. To make a real-life analogy, consider the following: Imagine that you invited a guest speaker from NASA. Imagine that everyone who works at NASA is of type NASAEmployee. In code, imagine that you instantiated a variable of type NASAEmployee named UnknownSpeaker. Then, you would have her speak: UnknownSpeaker.speak(); Now, you could relay this command before knowing WHO was speaking. But, at run-time, depending on who the speaker is, they may talk about different things. If you get an astronaut like Sally Ride, she might talk about what it's like to experience zero gravity for several days straight. If on the other hand you got a project manager, she might talk about how to create a time line. Since you don't know beforehand that you are going to get a speaker, it would be illegal to do the following: UnknownSpeaker.zerogravitydemo(); since only an Astronaut can do the zero gravity demo and when you make the initial invitation, you DON'T know that you're going to get an Astronaut. But since all NASA Employees can speak, it's perfectly valid when you extend the invitation to ask them to do so and then when the actual speech happens, it is tailored by the specific NASA Employee that comes to speak.
An example of a situation where polymorphism would be desirable is processing payroll. Each employee must get paid, but different employees get paid differently. An inheritance structure more easily encapsulates the differences between how everyone gets paid in a seamless manner than other systems. For example, you could take care of paying everyone as follows: Employee[] workers = new Employee[100]; // Stuff happens hereโฆ //Here we pay everyone: for (Employee worker: workers) worker.pay(); The neat thing here is that even though each array element is a reference to an Employee, the actual object to which is being referred could easily be a specialized Employee, such as a SalariedEmployee. Even though we might not know that workers[3] is a SalariedEmployee before run-time, when the program runs, it can pay the SalariedEmployee in the appropriate manner because the pay() would be overridden in the SalariedEmployee class. In general, it allows you to have a collection of general Objects and perform some action on all of them, but that action can be modified if some of the general Objects are of a more specific type.
We could potentially define four separate equals methods total (1) public boolean equals(Coordinate c); // in Coordinate (2) public boolean equals(ColorCoodinate c); // in Coordinate (3) public boolean equals(Coordinate c); // in ColorCoordinate (4) public boolean equals(ColorCoodinate c); // in ColorCoor. In the chart below, each column refers to one of the four methods above and each row represents a possible scenario of which methods are define. Here are some of the situations we'll consider: Situation Method #1 Method #2 Method #3 Method # 1 Yes No No No 2 Yes Yes No No 3 Yes No Yes No 4 Yes No No Yes 5 Yes Yes Yes No 6 Yes Yes Yes Yes One key idea to remember is the following: type matching (to determine which method gets called) for PARAMETERS is NOT dynamic. The compiler identifies the type of the parameter and that is what is used to determine what method gets called. In our situations above, it's clear what happens in situation #1. But after that it gets a bit more confusing, so let's try to lay down some rules to help use deal with the cases above, without having to remember 6 separate sets of rules.
To help explain the rules below, examples will be given. In each of the examples, assume that A inherits from Object, B inherits from A, C inherits from B, and D from C.
Let's apply our rules to situation #6 for the equals method in the Coordinate and ColorCoordinate classes. Here are the four objects initially created: Coordinate test = new ColorCoordinate(3,'a',"Green"); ColorCoordinate red = new ColorCoordinate(3,'a',"Red"); ColorCoordinate blue = new ColorCoordinate(3,'a',"Blue"); Coordinate nocolor = new Coordinate(3,'a'); Now, let's consider determining WHICH of the four methods gets called for each of the following calls: nocolor.equals(red): red is a ColorCoordinate reference, and since nocolor is referring to a Coordinate, the method that gets called is Method #2. red.equals(nocolor): nocolor is a Coordinate reference, and since red is referring to a ColorCoordinate, the method that gets called is Method #3. red.equals(blue): Method #4 gets called since both references are of type ColorCoordinate. test.equals(blue): blue is a ColorCoordinate reference, so initially, we match with Method #2, but using polymorphism, we see the test actually refers to a ColorCoordinate object, so Method #4 is executed. blue.equals(test): Here, method #3 gets executed because test is a Coordinate reference, and this doesn't dynamically get binded. However, if a method is called upon test in equals, polymorphism could occur for THAT call.