Docsity

Lecture Notes on Polymorphism - Object Oriented Programming | COP 3330, Study notes of Computer Science

Toggle sidebar
bg1
Polymorphism
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.
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lecture Notes on Polymorphism - Object Oriented Programming | COP 3330 and more Study notes Computer Science in PDF only on Docsity!

Polymorphism

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.

Motivation behind polymorphism

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.

Example: Coordinate & ColorCoordinate โ€“ equals

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.

General Rules for Polymorphism

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.

  1. First, you must find a method that matches the signature of the method called in the class of the reference of the object upon which the method was called. This, if the call to a method is of the form o.methodcall(parameters here) then there must be a method call in the class of the reference o OR a class that the class of the reference from o inherits from that fits the method signature. If there is not, then the code should produce a syntax error. Note: In finding a method call that matches, remember that the types of the actual parameters are based on the types of the references , and not based on the types of the objects they are referring to. So, for example, if the reference of o is of type B, and o is referencing an object of type C, then a method of the signature of the method call must be found in either the Object class, the class A or the class B. If it is ONLY in class C, then this would result in a syntax error.

Applying our Rules

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.

A couple exercises

  1. Imagine adding a ThreeDCC that extended ColorCoordinate, and adding two equals methods to it: one that takes in a Coordinate and another that takes in a ThreeDCC. (You can actually add this fairly easily.) In this situation, identify the equals methods that get called in the following sequence of code: Coordinate test = new ColorCoordinate(3,'a',"Green"); ColorCoordinate red = new ThreeDCC(3,'a',10,"Red"); ThreeDCC blue = new ThreeDCC(3,'a',79,"Blue"); Coordinate nocolor = new Coordinate(3,'a'); test.equals(red); red.equals(test); blue.equals(red); red.equals(blue); nocolor.equals(red); nocolor.equals(test); blue.equals(test);
  2. Actually write a small ThreeDCC class and test out your answers. You don't need to write a meaningful equals method, just enough to test the method calls above.