This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
class design problem
Posted by Alex on December 02, 2000 at 1:40 PM
> I am having a problem related to the design of classes using java as prog. language.The class heirarchy is like this: > GroupExpression (Parent class) > Min Max Avg Sum Count (Child classes) > These sub classes do the comparison of input parameters > depending on the class called(e.g Sum class will add the parameters,Count will count the params like that). > Parent class has Create(arg) and compute(arg1,arg2)methods.Child class will have the compute(arg1,arg2) method. > Caller will have refernce of Parent class and it will call its create()method to create an instance of reqd sub class.Then it will call the compute() method . > Now Problem is input parameters can be of type > int,float,long,double and accordingly the return type. > Now the code to check for the parameter type and accordingly select the return type or given that if return type is being passed then also the code becomes dirty. > Tried taking input to compute() method as an object having parameter type and parameter value as its attributes.But this again becomes clumsy. > Can you suggest me a neat way to overcome the problem. > Thanks and Regards, > Manish Dhall I would overload compute(). I'd have int compute(int a, int b), float compute(float a, float b), long .... etc... HTH. Alex.
Replies:
|