Consider the following example. I have a MyInterface interface, and then two abstract classes MyAbstractClass1 and MyAbstractClass2. MyAbstractClass1 implements MyInterface, but MyAbstractClass2 does not.
Now I have three specific classes.
- MyConcreteClass1 is derived from MyAbstractClass1, but does not implement MyInterface.
- MyConcreteClass2 is derived from MyAbstractClass2, but implements MyInterface.
- MyConcreteClass3 is derived from MyAbstractClass1 and implements MyInterface.
Is ConcreteClass1 also an implicit implementation of MyInterface because it comes from MyAbstractClass1? Assuming MyAbstractClass1 implicitly implements MyInteface methods, does ConcreteClass1 need not be passed to MyInterface to access MyInteface methods?
MyAbstractClass1 can implicitly implement the MyInterface method as an abstract method, but cannot explicitly implement the MyInterface method as an abstract method. Why is this?
Is MyConcreteClass3 excessive because it implements an interface that is already implemented by its base class? Will there be a reason why you would like to do this, even if you knew that all classes that come from MyAbstractClass1 should also implement MyInterface.
Here is the class diagram
alt text http://files.getdropbox.com/u/113068/abstractclassesandinterfaces.png
Here is the code:
public interface MyInterface
{
void MyMethodA();
void MyMethodB();
void MyMethodC();
}
public abstract class MyAbstractClass1 : MyInterface
{
public void MyMethodA()
{
}
void MyInterface.MyMethodB()
{
}
public abstract void MyMethodC();
public abstract void MyMethodZ();
}
public abstract class MyAbstractClass2
{
public void MyMethodX()
{
}
public abstract void MyMethodY();
}
public class ConcreteClass1 : MyAbstractClass1
{
public override void MyMethodC()
{
}
public override void MyMethodZ()
{
}
}
public class ConcreteClass2 : MyAbstractClass2, MyInterface
{
public override void MyMethodY()
{
}
public void MyMethodA()
{
}
public void MyMethodB()
{
}
public void MyMethodC()
{
}
}
public class ConcreteClass3 : MyAbstractClass1, MyInterface
{
public override void MyMethodC()
{
}
public override void MyMethodZ()
{
}
}
source
share