Mohanad M. Nader
Mohanad M. Nader

Reputation: 53

Is overriding of a function possible in the same class?

it's an objective question type, but Is overriding of a function possible in the same class? in general, and in C# to be exact. If it is possible how and when can it be used?

Upvotes: 5

Views: 6305

Answers (6)

kmxr
kmxr

Reputation: 479

It looks like it can't be overridden. However, I do see how it can be useful. For example, if one wanted to manually override machine-generated code.

Upvotes: 0

romanoza
romanoza

Reputation: 4862

class Test1
{
    public virtual void Test2() {

    }

    public override void Test2() {

    }
}

gives you a compilation error. So, no, it's not possible.

Upvotes: 1

Bilal Bashir
Bilal Bashir

Reputation: 1493

This is from the C# Language Specification §10.6.4 Override methods

The method overridden by an override declaration is known as the overridden base method. For an override method M declared in a class C, the overridden base method is determined by examining each base class type of C, starting with the direct base class type of C and continuing with each successive direct base class type, until in a given base class type at least one accessible method is located which has the same signature as M after substitution of type arguments. For the purposes of locating the overridden base method, a method is considered accessible if it is public, if it is protected, if it is protected internal, or if it is internal and declared in the same program as C.

A compile-time error occurs unless all of the following are true for an override declaration:

  • An overridden base method can be located as described above.

  • There is exactly one such overridden base method. This restriction has effect only if the base class type is a constructed type where the substitution of type arguments makes the signature of two methods the same.

  • The overridden base method is a virtual, abstract, or override method. In other words, the overridden base method cannot be static or non-virtual.

  • The overridden base method is not a sealed method.

  • The override method and the overridden base method have the same return type.

  • The override declaration and the overridden base method have the same declared accessibility. In other words, an override declaration cannot change the accessibility of the virtual method. However, if the overridden base method is protected internal and it is declared in a different assembly than the assembly containing the override method then the override method’s declared accessibility must be protected.

  • The override declaration does not specify type-parameter-constraints-clauses. Instead the constraints are inherited from the overridden base method. Note that constraints that are type parameters in the overridden method may be replaced by type arguments in the inherited constraint. This can lead to constraints that are not legal when explicitly specified, such as value types or sealed types.

So no it is not possible, the override modifier is used to extend the base class method not for hiding method definitions in the current class.

Maybe it is possible that your interviewer got confused by method selection after a method has been overridden, for example here.

Upvotes: 1

Nicolas C
Nicolas C

Reputation: 754

Well you can't. Per definition (from msdn): The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

You can't inherit of the class itself (see here) so you can't override in the same class.

One thing that is close to overriding and that you can do is method overloading.

public class A
{
    void methodA()
    {
        //something here
    }

    void methodA(int i)
    {
        //something else here
    }
}

Upvotes: 4

SalientBrain
SalientBrain

Reputation: 2551

Syntactically this is not possible using standart approach. But practically you can do it with changing delegates (more close to own methods). But this is not far away from changing behavior via objects, helpers (providing services by interface contacts) - which is more flexible.

Upvotes: 1

Paul Weiland
Paul Weiland

Reputation: 737

Just tested it. The class itself will not give the compiler a reason to show an error or even a warning but when you call the function the compiler shows:

The call is ambiguous between the following methods or properties: 'method1' and 'method2'

Upvotes: 0

Related Questions