Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of hiding (using the "new" modifier) an interface method declaration?

it's possible to mark a method declaration in an interface as "new" but does it have any "technical" sense or is it just a way to explicitly state that the declaration cannot override a previous one ?

For example :

interface II1
{
    new void F();
}

interface II2 : II1
{
    new void F();
}

is valid (the C# 4.0 compiler does not complain) but does not appear to be different from :

interface II1
{
    void F();
}

interface II2 : II1
{
    void F();
}

Thanks in advance for any information.

EDIT: do you know a scenario where hiding in an interface would be useful ?

EDIT: According to this link : Is method hiding ever a good idea (thanks Scott), the most common scenario seems to be the emulation of covariant return type.

like image 904
Pragmateek Avatar asked Aug 13 '10 12:08

Pragmateek


People also ask

What is the use of method hiding in C#?

In method hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.

Which modifier enables you to hide the compiler warning when a base class method and a derived class method have the same name?

Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier.

Why new keyword is used in C#?

When used as a declaration modifier, the new keyword explicitly hides a member that is inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base class version.

Why interface methods are public in Java?

Interfaces are meant to define the public API of a type - and only that, not its implementation. So any method (or static member) you define in an interface is by definition public . Since an interface can't contain any concrete implementation, there is no way to call any member methods from within.


2 Answers

The second example issues the following compiler warning:

'II2.F()' hides inherited member 'II1.F()'. Use the new keyword if hiding was intended.

I'd say the difference of using the new keyword is exactly that: showing intention.

like image 171
Fredrik Mörk Avatar answered Oct 04 '22 00:10

Fredrik Mörk


The two are very different. By using 'new' you are creating a new inheritance chain. This means any implementations of II2 will need to realize both versions of F(), and the actual one you end up calling will depend upon the type of the reference.

Consider the following three realizations:

    class A1 : II1
    {
        public void F()
        {
            // realizes II1.F()
        }
    }

    class A2 : II2
    {
        void II1.F()
        {
            // realizes II1.F()
        }

        void II2.F()
        {
            // realizes II2.F()
        }
    }

    class A3 : II2
    {
        public void F()
        {
            // realizes II1.F()
        }

        void II2.F()
        {
            // realizes II2.F()
        }
    }

If you have a reference to A2, you will not be able to call either version of F() without first casting to II1 or II2.

A2 a2 = new A2();
a2.F(); // invalid as both are explicitly implemented
((II1) a2).F(); // calls the II1 implementation
((II2) a2).F(); // calls the II2 implementation

If you have a reference to A3, you will be able to call the II1 version directly as it is an implicit implentation:

A3 a3 = new A3();
a3.F(); // calls the II1 implementation
((II2) a3).F(); // calls the II2 implementation
like image 32
Paul Ruane Avatar answered Oct 04 '22 00:10

Paul Ruane