Overwrite methods based on name only

Asked

Viewed 843 times

3

I’m creating an abstract base class to provide a pattern for other classes.

The base class methods shall be superscripted in the derived class.

Now comes the cat jump. The superscript should happen only based on the method name, without taking into account the parameters.

Example:

public abstrac class A
{
    public int AnyMethod()
    {
        throw new NotImplementedException ( "This method is not implemented" );
    }
}

public class B : A
{
    public override int AnyMethod( int a, int b)
    {
        return a + b;
    }
}

As you can see, my idea is to override a method from the parameter-independent base class, that is, based only on the method name.

That’s possible?

2 answers

5


No, that’s not possible, the method is not composed only by its name. Whenever the method signature is different it is another method. There is method overload (which is quite common, more than the operator overload). The example itself placed there shows that not, since it has no override.

Of course, there are ways to achieve a similar goal by going over language if it was really necessary, but if it is to do that it is better to use another language, than to create a sophisticated mechanism to circumvent what language purposely prevents.

To tell the truth you shouldn’t even use inheritance if you want the method to have different parameters, because in practice they do different things, then inheritance, or at least polymorphism, is the wrong mechanism.

What you can do, depending on the case, is use a public method with equal signature for inheritance and have private methods to have a more appropriate implementation in each class, then the inherited public method would call the private with different signature. Or you can think of some other mechanism that uses something similar.

I would say that all would be gambiarras to circumvent the philosophy of language. I just do not make a categorical statement because I do not know the concrete case. In some very specific case it is possible that such a solution can be used without problems. In this shown example it does not seem appropriate because the method is making a sum of two operands.

Note that using another signature, such as params, is doing something different than asking the question, that is to say the signature has been changed so that the override occur based on the whole and not just the name. There is a difference between parameters and arguments.

The use of modifier new, that doesn’t make the envelope, does not have the slightest use for this problem since it would only hide the base method that has the same signature, in a different signature method its use is unnecessary and the operation is exactly the same without the modifier, after all this is a new method by definition.

Launch a NotImplementedException is rarely a good idea, certainly not in a method that serves as the basis.

I ignored small mistakes that maybe exist only in the example, obviously they will not be committed in the specific case, nor would compile.

2

In this case, we will have to be creative. Simply ignoring the arguments extrapolates the design of the C language#.

First, the method, to be overwritten, needs to be virtual:

public virtual int AnyMethod()
{
    throw new NotImplementedException ( "This method is not implemented" );
}

Second, it is possible to make an implementation that receives N arguments of a certain type. Or, still, of object, but that if you like to live with emotion:

public virtual int AnyMethod(params int[] numeros)
{
    throw new NotImplementedException ( "This method is not implemented" );
}

Third, you can rather go on deriving polymorphisms with different arguments in the derived class. Something like this:

public class B: A
{
    public override int AnyMethod(params int[] lista)
    {
        return lista.Sum(x => x);
    }

    public int AnyMethod(int a, int b) 
    {
        return AnyMethod(new int[] {a, b});
    }
}

Then we can have a specific method with 2 arguments and another with N.

public class Program
{
    public static void Main()
    {
        Console.WriteLine(new B().AnyMethod(1, 2));
        Console.WriteLine(new B().AnyMethod(1, 2, 3));
    }
}

I made you a Fiddle.


At the request of the questioner, I will also explain one more option, which is the reintroduction of methods, made by the word modifier new in the method.

new would be the equivalent of reintroduce of Delphi, where we explicitly override a base class method in the derived class. The difference to override is that the base class method is can be called using base.AnyMethod, while using new the base class method is totally ignored.

In the scope of the example, if there was a method public int AnyMethod(int a, int b) in the base class, it would be something like:

public new int AnyMethod(int a, int b) 
{
    return a + b;
}

This type of operator, as well as the addition of more polymorphisms in derived classes, can be prevented by using the modifier sealed in the class declaration.

I upgraded Fiddle by now placing a polymorphism for three operators.

  • So I also posted this question on the MSDN forum, and I got an interesting answer. solution? Proceeding?

  • 1

    Yes, this is what is called "method reintroduction". If you like, I can expand this answer by explaining this better.

  • 1

    Of course, I always prefer the answers here in the stack, because I find the class here more patient. And the answers are always more detailed.

  • Reply and Fiddle updated. If you need more details, just say.

  • In the MSDN example the author reintroduces a method from the base class, but in the new version he adds two parameters to the method that does not exist in the base class version. In this case, the new modifier is actually overwriting the inherited method or is just creating an overload?

  • 1

    In his answer, the new modifier is unnecessary. If you try modifying Fiddle to make it look like this, you will see that Fiddle will warn you that the modifier new is not necessary. Therefore, he is only creating a polymorphism for the method. When we talk about overload, we’re talking about operators, not exactly methods.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.