Polymorphism, Exercises


Explain what the difference is between the concept of polymorphism and concept of dynamic binding. Why is polymorphism a pre request for dynamic binding?


Create the Shape class hierarchy and add a method tell() to the Shape base class do not override this method in the subclass. Create an object of each of the classes in the Shape hierarchy in a main() method and call the tell() method. Now add the tell() method to a single of the subclasses and execute the main() method again. Explain the change. Finally, override the tell() method in all the subclasses and execute the main() method. Again, explain the change.


Reuse the Shape hierarchy with the tell() method implemented on all classes. Create an array of Shapes and store different types of Shapes in the array. Loop over the array and call the tell() method. Explain what is happening.


In an abstract class it is possible to have concrete methods, i.e., methods with a body (and naturally abstract methods). List the advantages and disadvantages of being able to have concrete methods in abstract classes.


Modify the Shape hierarchy from Exercise C but make Shape an abstract class. In addition make relevant methods on Shape abstract.

Extra Assignments


Modify Exercise C so that it demonstrates the order of initialization of the base and derived classes.


Create a class with two methods setup() and teardown(). Make an inheritance hierarchy that is three levels deep. In the derived classes implement the setup() method such that it is called in a general-to-specific order. Implement the teardown() method such that it is called in a specific-to-general order.