1

Here's what I am trying to do:

At the top sits an interface:

interface IHasPosition
    {
        Position Position
        {
            get;
        }
    }

next up, we have an abstract class:

abstract class Person : IHasPosition
    {
        public abstract Position Position
        {
            get;
        }
    }

Finally, I extend Person:

class Instructor : Person
    {
        public override Position Position
        {
            get { return new Position(1, 1); }
        }
    }

This compiles just fine. However, I seem to be unable to explicitly define the property Position. Something like this:

class Instructor : Person
    {
        public override Position IHasPosition.Position
        {
            get { return new Position(1, 1); }
        }
    }

This fails to compile:

Error   1   'ConsoleApplication3.Instructor.IHasPosition.Position': virtual or abstract members cannot be private
Error   3   The modifier 'override' is not valid for this item
Error   2   The modifier 'public' is not valid for this item
3
  • Why do you want to explicitly implement the interface's property? Commented Oct 4, 2013 at 13:36
  • The modifier 'public' is not valid for this item - you are trying to implement it explicitly; The modifier 'override' is not valid for this item - again related to first statement Commented Oct 4, 2013 at 13:43
  • what are you trying to do exactly?i mean why abstract implementation on base class and private in derived?or do you want public in derived? Commented Oct 4, 2013 at 16:00

2 Answers 2

3

Instructor is inheriting Person and Instructor.Position is overriding Person.Position.

Since Person.Position already implements IHasPosition.Postion, so does Instructor.Position.

Instructor is a Person is a IHasPosition.

Explicit implementation of the interface IHasPosition.Position on Instrcutor is unnesscessary and, would be misleading.

Lets possit that you do explicitly implement the interface that has already been inherited. On the override it is superfluous, and illegal because its public, on another member it creates an ambiguity.

Sign up to request clarification or add additional context in comments.

Comments

1

There's a syntax error in your code: explicit interface implementation can't be public. Technically, you can put it like that:

   class Instructor: Person {
     // From Person abstract class
     public override Position Position 
     {
       get { return new Position(1, 1); }
     }

     // Explicit interface implementation
     Position IHasPosition.Position 
     {
       get { ... }
     }
   }

But as Jodrell said, it's a very bad style.

2 Comments

+1 for showing you can actually do it. (-1 to anybody who actually does.)
That still doesn't seem to work. It complains about the containing type not implementing IHasPosition: Error 'ConsoleApplication3.Instructor.IHasPosition.Position': containing type does not implement interface 'ConsoleApplication3.IHasPosition'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.