Why do abstract classes need to implement interface members?#

Why can't I do this in C#:

public abstract class Base : ISerializable { }
The compiler complains that Base doesn't implement the methods necessary for the ISerializable interface. But who cares, it's an abstract class - it's not supposed to implement all members. It would make sense to me to be able to require that derived classes of my base class implement certain interfaces but without providing abstract methods for all of their members already.

Now I have to go:

public abstract class Base : ISerializable
{
    public abstract void GetObjectData(SerializationInfo info,
StreamingContext context);
}
which is pretty lame if you need to do this for a number of methods or interfaces...

Thursday, May 03, 2007 12:16:10 PM (Romance Standard Time, UTC+01:00)
Hi!

I agree. Let's say that interface A has three methods:

interface A { void A(); void B(); void C() }

and in your abstract base you only implement method A and B:

class Base
{
public void A(){...}
public void B(){...}
public abstract void C();
}

Since you want your subclasses to reuse A and B and have various implementation for each subclass for method C then this make sense. The interface will be used to expose the class to the consumer code, so it will not know what instance it is invoking methods on.

I cannot understand why this is not possible in C#, it sure is in Java.
Comments are closed.
All content © 2012, Jelle Druyts
On this page

Recent Photos
www.flickr.com
This is a Flickr badge showing public photos from Jelle Druyts. Make your own badge here.
Advertising
Top Picks
Statistics
Total Posts: 350
This Year: 0
This Month: 0
This Week: 0
Comments: 530
Archives
Sitemap
Disclaimer
This is my personal website, not my boss', not my mother's, and certainly not the pope's. My personal opinions may be irrelevant, inaccurate, boring or even plain wrong, I'm sorry if that makes you feel uncomfortable. But then again, you don't have to read them, I just hope you'll find something interesting here now and then. I'll certainly do my best. But if you don't like it, go read the pope's blog. I'm sure it's fascinating.

Powered by:
newtelligence dasBlog 2.0.7226.0

Sign In