I am trying to create a tree structure (a binary tree) that is capable of holding two different types of class (a sphere and a rectangle).
For obvious reasons my sphere and rectangle will have different methods for getting their size (getSize()) and I also intend to have a constructor (for both classes) thats takes two objects (two spheres OR two rectangles) and combines them to create a larger sphere or rectangle.
How should I approach coding a node so that it can store either a sphere or a rectangle at a node calling the appropriate methods when required?
Would a simple interface accomplish this if i cast objects to the type i need?
Thanks,
DMcB
I would create three classes.
An abstract class Shape that contains all common code for rectangles and spheres.
All shape specific methods should be left abstract such as
getSize(),drawShape(),mergeShape().Also, maybe not relevant but the Composite design pattern might be of good use for this problem