Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help understanding abstract classes that implement an interface

Consider the following example. I have an interface MyInterface, and then two abstract classes MyAbstractClass1 and MyAbstractClass2. MyAbstractClass1 implements MyInterface, but MyAbstractClass2 does not.

Now I have three concrete classes.

  1. MyConcreteClass1 is derived from MyAbstractClass1 but does not implement MyInterface.
  2. MyConcreteClass2 is derived from MyAbstractClass2, but does implement MyInterface.
  3. MyConcreteClass3 is derived from MyAbstractClass1, and does implement MyInterface.

Does ConcreteClass1 also implicitly implement MyInterface because it derives from MyAbstractClass1? Assuming MyAbstractClass1 implements the methods of MyInteface implicitly then ConcreteClass1 should not have to be cast to a MyInterface to access the MyInteface methods right?

MyAbstractClass1 can implicitly implement a method of MyInterface as an abstract method, but can't explicitly implement a method of MyInterface as an abstract method. Why is this?

Is MyConcreteClass3 excessive because it's implementing an interface that is already implemented by its base class? Would there be a reason you would want to do that even if you knew all classes that derive from MyAbstractClass1 should also implement MyInterface.

Here's a class diagram

alt text http://files.getdropbox.com/u/113068/abstractclassesandinterfaces.png

Here's the code:

//interface
public interface MyInterface
{
    void MyMethodA();
    void MyMethodB();
    void MyMethodC();

}

//abstract classes
public abstract class MyAbstractClass1 : MyInterface
{
    public void MyMethodA()
    {
    }


    void MyInterface.MyMethodB()
    {

    }

    //Error: "the modifier abstract is not valid for this item"
    //abstract void MyInterface.MyMethodC();

    //This works
    public abstract void MyMethodC();

    public abstract void MyMethodZ();

}


public abstract class MyAbstractClass2
{
    public void MyMethodX()
    {
    }

    public abstract void MyMethodY();

}

//Concrete classes
//ConcreteClass 1: Only Abstract class implements the interface
public class ConcreteClass1 : MyAbstractClass1
{
    public override void MyMethodC()
    {
    }

    public override void MyMethodZ()
    {

    }
}

//ConcreteClass 1: Only Concrete class implements the interface
public class ConcreteClass2 : MyAbstractClass2, MyInterface
{
    public override void MyMethodY()
    {
    }

    public void MyMethodA()
    {

    }

    public void MyMethodB()
    {

    }

    public void MyMethodC()
    {

    }

}
//ConcreteClass 1: Both concrete and abstract class implement the interface
public class ConcreteClass3 : MyAbstractClass1, MyInterface
{
    public override void MyMethodC()
    {

    }

    public override void MyMethodZ()
    {

    }
}
like image 399
Eric Anastas Avatar asked Jan 23 '26 21:01

Eric Anastas


2 Answers

Does ConcreteClass1 also implicitly implement MyInterface because it derives from MyAbstractClass1?

Yes.

ConcreteClass1 should not have to be cast to a MyInterface to access the MyInteface methods right?

Correct. (myConcreteClass1 is MyInterface) will evaluate true.

MyAbstractClass1 can implicitly implement a method of MyInterface as an abstract method, but can't explicitly implement a method of MyInterface as an abstract method.

Explicit implementation is to distinguish between overlapping member signatures. The explicit implementation is private to the class you are implementing it on, so it is not accessible to derived classes (and thus cannot be abstract). You also cannot force classes which derive from MyAbstractClass1 to explicitly implement MyInterface, so there is no way to ensure the abstract member will ever be implemented.

Is MyConcreteClass3 excessive because it's implementing an interface that is already implemented by its base class? Would there be a reason you would want to do that even if you knew all classes that derive from MyAbstractClass1 should also implement MyInterface.

Not necessarily, If you need to explicitly implement a member of the interface to distinguish it from an overlapping member on MyConcreteClass3. Otherwise it is unnecessary.

like image 109
Rex M Avatar answered Jan 25 '26 11:01

Rex M


In this case, all three classes implement the interface (directly or indirectly). This is because MyAbstractClass1 implements MyInterface, and since MyConcreteClass1 derives from MyAbstractClass1, it also follows that you can treat MyConcreteClass1 as a MyInterface. MyConcreteClass2 can be treated with something that derives from MyAbstractClass1, as long as you treat it as a MyInterface. the derivation from MyInterface in ConcreteClass3 is a bit redundant since MyAbstractClass1 already implements MyInterface.

With all of that information, i'd say that yes, it is redundant to implement MyInterface on MyConcreteClass3 since it derives from MyAbstractClass1 which already implements MyInterface. I think the reason that you cant have an abstract implementation of an interface method is that it provides no code itself and you cannot guarantee that it will be overriden in subcalsses. Use Virtual instead.

like image 42
RCIX Avatar answered Jan 25 '26 11:01

RCIX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!