CSSE 220: Polymorphism
The purpose of this exercise is to experiment with polymorphism.
Upon completion, you should have a very good understanding of
- polymorphism
- type compatibility of derived classes and their base class
- how super is used to invoke methods of a base class
- abstract methods and classes
- static overloading
Feel free to work on this exercise in pairs.
Please use the following document to
provide your answers and turn
in a paper copy after your are finished with this exercise.
- Download and unzip the PersonDemo
project. If you have problems, your project path maybe
set incorrectly. Try changing it.
- Study the Employee class. Which class does it extend?
- In the main() method of the PersonDemo class,
where indicated, place the following assignment statement: p =
s; Compile and run. What happens to p and s and why
can you make the assigment?
- In the line after the above statement, insert the assignment
assignment statement: p = e; Compile and run. What happens to p
and e and s in the two statements that you
added? The effect you observed is called polymorphism. In your
own words, what is polymorphism? [Hint: It's OK to consult the book on
this and any of the other questions.]
- Remove the second statement that you added and insert the
following statement instead: a = p;
- Attempt to compile and run. Why does this code not work. What
are we attempting to accomplish here?
- After you experiment with your code, edit the inserted
statement
so that it reads: a = (Student) p;
- Compile and run. What happened? Explain the term downcasting.
- Remove the statement: p = s;
- Compile and run. You will get an error message. Explain why
you get it. [Hint: "Because the computer does not like me" is not an
acceptable answer.]
- Remove the code you added and instead insert the following three
lines:
p = s;
p.loves(s);
p.loves(p);
- There are four loves methods: two in Person
and two in Student.
- Study the four methods carefully. Note the differences
in their outputs!
- Compile and run your program. Which of the four loves
methods got called by the first loves call above? By the second
loves call above?
- Why does Java not call the exact same method in both
cases?
- The phenomena you encountered are called dynamic binding
and static overloading. In your own words, what is "dynamic
binding" and what is "static overloading"?
- Consider the method toString() in the Student
class. We have already seen the use of the keyword super in the
context of constructors. Explain the purpose of super in this
method.
- Declare an array that may hold either Person, Student,
or Employee objects. Populate the array
with
five objects, ensuring that all three classes are represented. Write a
loop which prints out the name, age, phonenumber, and gpa or salary of
all five objects.
- Consider the prior exercise. For each object, indicate which
method of which class is used to print out each object. Explain why
this is a case of dynamic binding.
- Modify the loop from (8) and only print out the GPA, if the
object is a student. [Hint: Use the instanceof operator.]
- Uncomment the "method" isCool() in the Person
class.
- What is unusual about it?
- What is an abstract method?
- Compile your project, you get an error message. The error
message is very good. Fix your code as suggested in the error
message. The rule is that if you have at least one abstract method in
a class, then the class has to be declared "abstract." The implication
of declaring a class abstract is that you cannot instantiate
it. Modify your project so that it does not instantiate a Person
object. Modify the Student and Employee classes by
defining the method isCool() in them. Be creative. Test your
code.
- In which cases may we be interested in "abstract" methods?
- Explain how interfaces relate to abstract classes.