views:

663

answers:

7

I have a strange problem that I could not solve. When I try to compile the following snipped I get this error:

'AbstractClass' does not implement interface member 'Property' (Compiler Error CS0535)

The online help tells me to make my AbstractClass abstract, which it is. Can anybody tell me where I went wrong?

Cheers Rüdiger

public interface IBase {
    string Property { get; }
}

public abstract class AbstractClass : IBase
{
    public override string ToString()
    {
        return "I am abstract";
    }
}

public class ConcreteClass : AbstractClass
{
    string Property { 
        get {
            return "I am Concrete";
        }
    }
}
+10  A: 

Your AbstractClass needs to provide an implementation for Property from the IBase interface, even if it's just abstract itself:

public abstract class AbstractClass : IBase
{
    public override string ToString()
    {
        return "I am abstract";
    }

    public abstract string Property { get; }
}

Update: Luke is correct that the concrete implementation will need to specify that Property is an override, otherwise you'll get a "does not implement inherited abstract member" error:

public class ConcreteClass : AbstractClass
{
    public override string Property { 
        get {
            return "I am Concrete";
        }
    }
}
dahlbyk
Thank you, that obviously solves it. I am still wondering why, it seems to be a break with the usual way Interfaces are handled in abstract classes.
Rüdiger
How do you mean? A subclass of your abstract class shouldn't necessarily need to know that it implements IBase, it should just see virtual and abstract members that it may/must implement.
dahlbyk
You'll also need to declare `Property` on `ConcreteClass` with the `override` modifier.
LukeH
You are right dahlbyk. Seems I misunderstood the concept of interfaces and abstract classes. I thought only a concrete class needed to implement all abstract and interface members of base classes.
Rüdiger
Well it works like that for interfaces implementing interfaces, but really the only special thing about an abstract class is that it can have abstract methods and not be constructed directly. All other rules apply.
dahlbyk
A: 

AbstractClass is supposed to implement IBase which contains Property, and you haven't implemented it

Charlie
+1  A: 

You need to implement the IBase-property Property like this:

public abstract class AbstractClass : IBase
{
    public override string Property()
    {
        return "This is the base-class implementation";
    }
}

Or make it abstract.

Seb Nilsson
+2  A: 

You abstract class does not implement the interface IBase. Just add the property Property to AbstractClass.

public abstract String Property { get; }
Daniel Brückner
A: 

You must implement Property property in abstract class.

Vytautas
+2  A: 

You have to add abstract implementation of the property to your AbstractClass :

public abstract class AbstractClass : IBase
{
    public override string ToString()
    {
        return "I am abstract";
    }

    public abstract string Property { get; }

}

And the override keyword to the Concrete class

Grzenio
+2  A: 

You need to declare Property in AbstractClass so that it fulfills the IBase contract.

If you want ConcreteClass to be able to override Property then you should declare it as abstract or virtual. Then you'll need to declare the ConcreteClass implementation of Property with override.

    public interface IBase
    {
        string Property { get; }
    }

    public abstract class AbstractClass : IBase
    {
        public abstract string Property { get; }

        public override string ToString()
        {
            return "I am abstract";
        }
    }

    public class ConcreteClass : AbstractClass
    {
        public override string Property
        {
            get
            {
                return "I am Concrete";
            }
        }
    }
LukeH