You are on page 1of 1

This document is part of the RMIT Online course, Programming 1.

Task 7.2.3 Practice Exercises


Using Polymorphism

For this exercise you are required to use your imagination!

Make up three classes that have a hierarchical relationship where one class is the superclass
and the other two classes are direct subclasses (This is similar to question 1 above where
Account was the superclass and SAccount and CAccount were both direct subclasses).

Next you should write some code based on these classes as follows (NOTE: Try to use Eclipse
code generation as much as you can):

• Provide a simple implementation of each class that has at least one shared attribute in the
superclass and one different attribute in each subclass.
• Create a constructor in each subclass which calls the superclass constructor.
• Override the toString() method in all three classes. The two subclass implementations
should call super.toString() and add something extra to it. It is up to you what string
representation you create but it should be human readable and meaningful and based on
the relevant class attributes.
• Create a separate test class with a main() method.
• In main() create an instance of each of the two subclasses and place them in an Array of
type SuperClass[] (where SuperClass is the name of your specific superclass) ..
NOTE: see if you can do this in one statement!
• Iterate through the collection using a loop and use println to display the toString()
value of each item in the array (this is polymorphism at work!).

Once finished compare your solution with another student and see if you can agree on who came
up with the best example :)

You might also like