Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make an abstract method's parameter list have overridable length and types?

Is it possible to create a base class like the following:

 public abstract class baseClass
{
     public abstract void SetParameters(/*want this to be adjustable*/);
}

so that classes that override it can define the parameters required? In other words, I want to force the method to be overridden, but leave it up to the overriding class what is required here - so one might be

 public class derivedClass1 : baseClass
{
     public override void SetParameters(Point a, int b);
}

whereas another could be

 public class derivedClass2 : baseClass
{
     public override void SetParameters(List<Line> a, Point b, Point c, bool e);
}

?

Thanks for any help you can give

like image 523
simonalexander2005 Avatar asked Jan 29 '11 19:01

simonalexander2005


1 Answers

Absolutely not - that would break half the point of having the abstract method in the first place - no-one would be able to call it, because they wouldn't know which method signature had actually been written. The whole point of an abstract class is that a client can have a reference of type BaseClass without caring about what the type of the actual implementation is.

If the base class is able to predict in advance which types might be involved, one possibility to make life easier for the caller is to have the most general signature (typically the one with the most parameters) abstract, and make various overloads which call that general one providing defaults for the parameters that the client hasn't specified.

like image 127
Jon Skeet Avatar answered Nov 09 '22 11:11

Jon Skeet