Abstract/Interface question
-
Friday, August 17, 2007 3:17 PM
I'm using abstract classes to enforce implementation of certain methods. At the same time, I am also requiring derivitives to implement methods of an common interface (this interface is used elsewhere also, so I don't want to change that part).
I'm getting inconvenienced by a compiler error, illustrated by the following code:
Code Snippetpublic interface IMyInterface
{
void bob();}
public
abstract class Foo : IMyInterface{
}
public
class Bar : Foo{
void bob(){
}
}
The error is:
error CS0535: 'Foo' does not implement interface member 'IMyInterface.bob()'
From my perspective, I don't want class Foo to implement bob(). It's an abstract class, and the responsibility that bob() is implemented should be given to the derived class. So 1.) Is this an oversight in C#? and 2) any suggestions on how to get around this?Thanks,
Brian
Answers
-
Friday, August 17, 2007 3:48 PM
If you say that Foo will implement IMyInterface, you have to make sure that the slots for the members on IMyInterface exist in Foo.
By declaring the bob() message as abstract, you create such a slot, but because it's abstract, the actual implementation will be forced upon derivatives...
Code Snippetpublic abstract class Foo : IMyInterface
{
public abstract void bob();
}
// we want to create instances of Bar, thus we have to make sure there are no abstract members anymore...
class Bar : Foo
{
public override void bob() { }
}
All Replies
-
Friday, August 17, 2007 3:48 PM
If you say that Foo will implement IMyInterface, you have to make sure that the slots for the members on IMyInterface exist in Foo.
By declaring the bob() message as abstract, you create such a slot, but because it's abstract, the actual implementation will be forced upon derivatives...
Code Snippetpublic abstract class Foo : IMyInterface
{
public abstract void bob();
}
// we want to create instances of Bar, thus we have to make sure there are no abstract members anymore...
class Bar : Foo
{
public override void bob() { }
}
-
Friday, August 17, 2007 4:15 PM
Ah, that's the trick... I'm wondering if I'm being dense on this, or this is not an obvious thing to do. In any case, I'm good to go. Thanks, Tim! -
Tuesday, August 28, 2007 2:47 PM
No you are not being dense. This is a very strange solution, and I wonder if there is a reason why they wanted it this way. If I do the same thing in Java then I dont have to add abstract stubs to the abstract class.
-
Monday, September 17, 2007 12:49 PMI agree. I just got stuck on this, I don't see why I need to declare the methods a second time. Seems like more code to fix if I want to make changes later, but no benefit.
-Scott -
Tuesday, August 12, 2008 7:23 PMI disagree my friends.
If you do not want to implement bob() in your abstract class,
the correct way is to declare your interface as abstract interface
and the method abstract method, that's it.
let me know your thoughts. -
Tuesday, August 12, 2008 7:29 PMModerator
You cannot implement an abstract interface. Try compiling the following:
public abstract interface IMyInterface
{
}
It will not compile.
David Morton - http://blog.davemorton.net/ -
Tuesday, August 12, 2008 7:32 PMHi Dave,
it does not compile in C#?
then that is the problem with C#.
It is perfectly legal in JAVA... and that is the only reason why one would use an abstract interface. -
Tuesday, August 12, 2008 7:41 PMplease see for example:
http://www.ph.ed.ac.uk/~adk/java/qref11/java.sql.Connection.html

