If the method getMyColor()
is the same in each of your 10 classes, which it sounds like is true, it would be best to define an interface with the method defined there. As long as your classes implement the interface, I *think* you don't need to provide the method getMyColor()
within them. They will implicitly have it since they implement the interface. Could someone verify/correct me on this?
If the method definitions are different at all between your 10 classes, an abstract super-class would be best, with the method getMyColor()
declared abstract with no definition. Then you are forced to define the method in each class extending the super-class. I have described the interface way above...the way you would do it using an abstract super-class is very similar:
Declare your abstract super-class, with one abstract method in it named getMyColor()
(with no definition). All your other classes already define that method, so the only change needed for them is to make them extend SuperClass
. Then, in your method newClass
, just pass in the name of your abstract super-class.
Very little difference from using an interface, as you can see. I think using an interface would still be your best bet though, at least for future re-usability or adding onto your code, if nothing else.
Anyone else have any comments on this? I'm definitely no expert on the matter by any means, and would appreciate clarification/verification myself
- Ryan Burgdorfer
- Java Acolyte
[This message has been edited by ryan burgdorfer (edited April 28, 2001).]