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?
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.
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.
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.
Yes, you can. But you need to declare extends before implements : public class DetailActivity extends AppCompatActivity implements Interface1, Interface2 { // ... }
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)
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With