Generics and abstract class
For the sake of making the question simple, consider an abstract class '3Dshapes' with an abstract method 'getVolume'. Also consider two inherited classes, sphere and cylinder. For sphere I would need one variable for volume (radius). For the cylinder I would need two (radius, length). How can I define the abstract method in 3Dshapes to allow for a different number of parameters? How about different data types?
A follow up; I can't think of why I would use it for shapes, but what if the abstract method were a function instead of a sub and I wanted the freedom to return different data types?
Thanks;
Just a note: I code in C# so my terminology may be a little different. I'll try to use generic terms.
If the method getVolume() is an instance method, then it would not take any parameters. It would simply compute the volume of the shape using the appropriate instance data. So, in this example the method would require no arguments and the problem goes away.
I cannot think of an example off the top of my head, but it could still be possible that an abstract method normally had some number of parameters, but for some members of the hierarchy it needed more. In that case, it would be useful to have the normal number of parameters do something logical -- such as use a default for the remaining parameters, or throw an exception -- and then provide overridden methods with the additional forms.
For your follow-up, the answer, before generics, wass to have the function return the abstract type.
Thanks for the reply,
Certainly I could, for example, have the radius and length be set as properties instead of including them in the 'getVolume' method. The problem with that is that it doesn't programmaticly force the radius to be defined before the volume method is called.
An override of no parameters, with an overload following it is not so desirable as well. My application is for machine control. I don't like the idea of using 'defaults' with respect to motor directions and speeds.
I used 3Dshapes to make the question easier to ask. For my application, however, it would be nice to pass the parameters in the method call.
I think that I have seen something done with invoking delegates, and passing parameters to threads, where in the parameter list was something like new object()("one","two"), but I can't seem to find the examples anymore.