Polymorphism, Exercises

A

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

B

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.

C

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.

D

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.

E

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

Extra Assignments

E1

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

E2

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.