Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whether to extend interface, when base class already extends same interface

In C#, as shown in the code snippet below, is it correct/proper to extend the interface IFoo when declaring the class A, knowing that the class BaseClass extends the interface IFoo? Is it necessary to specify the interface IFoo here, and is it best practice?

class A : BaseClass, IFoo
{
}

Might be a silly question, but what is the appropriate practice in this case?

like image 611
Osama Avatar asked Dec 22 '18 16:12

Osama


People also ask

Can I extend a class that implements an interface?

Note: A class can extend a class and can implement any number of interfaces simultaneously. Note: An interface can extend any number of interfaces at a time.

What is the difference between implementing an interface and extending a class?

Difference: implements means you are using the elements of a Java Interface in your class. extends means that you are creating a subclass of the base class you are extending. You can only extend one class in your child class, but you can implement as many interfaces as you would like.

When to use extend and implement in Java?

The keyword extends is used when a class wants to inherit all the properties from another class or an interface that wants to inherit an interface. We use the implements keyword when we want a class to implement an interface.

Can I use extends and implements together in Java?

Yes, you can. But you need to declare extends before implements : public class DetailActivity extends AppCompatActivity implements Interface1, Interface2 { // ... }


2 Answers

If the BaseClass is Inherited from IFoo it is totally unnecessary to use IFoo in your Class A.

Check the image below (Resharper is used for this recommendation)

enter image description here


Special thanks to @InBetween

If interface reimplementation is the case, re-defining interface on child class has use case.

interface IFace
{
    void Method1();
}
class Class1 : IFace
{
    void IFace.Method1()
    {
        Console.WriteLine("I am calling you from Class1");
    }
}
class Class2 : Class1, IFace
{
    public void Method1()
    {
        Console.WriteLine("i am calling you from Class2");
    }
}

int main void ()
{
    IFace ins = new Class2();
    ins.Method1();
}

This method returns i am calling you from Class2

whereas,

interface IFace
{
    void Method1();
}
class Class1 : IFace
{
    void IFace.Method1()
    {
        Console.WriteLine("I am calling you from Class1");
    }
}
class Class2 : Class1
{
    public void Method1()
    {
        Console.WriteLine("i am calling you from Class2");
    }
}

int main void ()
{
    IFace ins = new Class2();
    ins.Method1();
}

returns I am calling you from Class1

like image 103
Derviş Kayımbaşıoğlu Avatar answered Oct 07 '22 22:10

Derviş Kayımbaşıoğlu


Although the accepted answer is correct in your particular scenario, this is not always the case.

Redeclaring the interface in the class declaration can be useful and necessary: when you want to reimplement the interface.

Consider the following code, study it carefully:

interface IFoo {
    string Foo(); }

class A: IFoo {
    public string Foo() { return "A"; } }

class B: A, IFoo {
}

class C: A {  
    new string Foo() { return "C"; } }

class D: A, IFoo {
    string IFoo.Foo() { return "D"; } }

And now try to figure out what the following code will output:

IFoo a = new A();
IFoo b = new B();
IFoo c = new C();
IFoo d = new D();

Console.WriteLine(a.Foo());
Console.WriteLine(b.Foo());
Console.WriteLine(c.Foo());
Console.WriteLine(d.Foo());

Do you now see how redeclaring the interface (type D) can be useful?

Also, another good point to make is how the information in MSDN can be misleading seemingly implying that many interfaces are redeclared without any apparent reason in a lot of classes; many collection types for instance redeclare infinite amount of interfaces.

This is really not true, the problem is that the documentation is built upon the assembly's metadata and the tool can't really discern if the interface is declared directly in the type or not. Also, because its documentation, explicitly telling you the implemented interfaces, regardless of where they are actually declared, is a bonus even if its not 100% accurate with the source code.

like image 40
InBetween Avatar answered Oct 07 '22 21:10

InBetween