CSSE 220: Polymorphism

The purpose of this exercise is to experiment with polymorphism.

Upon completion, you should have a very good understanding of

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.

  1. Download and unzip the PersonDemo project.  If you have problems, your project path maybe set incorrectly. Try changing it.

  2. Study the Employee class. Which class does it extend?

  3. 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?

  4. 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.]

  5. Remove the second statement that you added and insert the following statement instead: a = p;

    1. Attempt to compile and run. Why does this code not work. What are we attempting to accomplish here?

    2. After you experiment with your code, edit the inserted statement so that it reads: a = (Student) p;

    3. Compile and run. What happened? Explain the term downcasting.

    4. Remove the statement: p = s;

    5. 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.]

  6. Remove the code you added and instead insert the following three lines:

    p = s;
    p.loves(s);
    p.loves(p);

    1. There are four loves methods: two in Person and two in Student.
      • Study the four methods carefully. Note the differences in their outputs!

    2. 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?

    3. Why does Java not call the exact same method in both cases?

    4. The phenomena you encountered are called dynamic binding and static overloading. In your own words, what is "dynamic binding" and what is "static overloading"?

  7. 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.

  8. 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.

  9. 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.

  10. Modify the loop from (8) and only print out the GPA, if the object is a student. [Hint: Use the instanceof operator.]

  11. Uncomment the "method" isCool() in the Person class.

    1. What is unusual about it?

    2. What is an abstract method?

    3. 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.

    4. In which cases may we be interested in "abstract" methods?

  12. Explain how interfaces relate to abstract classes.