4

Is it possible to implement the interface in an abstract class and change the implemented interface method into abstract?

interface ITest
{
    string GetData();
    int ProcessData();
}

public abstract class MyAbstract:ITest
{
    public int Process()
    {
        // some code
    }

    public abstract string GetData(); // Change the implemented method into abstract Is it Possible?
}
5
  • probably duplicate of this stackoverflow.com/questions/1395298/… Commented Dec 2, 2011 at 6:17
  • Thanks for your immediate replies guys, but my question is if i implement interface method and change that method into abstract is possible or not. public abstract string GetData() Commented Dec 2, 2011 at 6:27
  • Are you asking whether it's possible to change a non-abstract method into an abstract one? Commented Dec 2, 2011 at 6:30
  • did you read all the answers of the above post ? your answer is there in that post. Commented Dec 2, 2011 at 6:33
  • @FosterZ: I don't believe it's a dupe because: a) that question is not constructive, b) the example given in that question doesn't deal specifically with having abstract members implementing the interface, it only revolves around with implementing the interface on the abstract class in any way (not whether or not it's actually possible, and it doesn't give as an example this case); this question deals specifically with that case. Commented Dec 2, 2011 at 17:24

1 Answer 1

1

Yes, you can do it. Simply add the abstract keyword and remove the implementation. There's obviously a pitfall with this. Any class which inherits from your abstract class will have to implement GetData themselves now. Depending on how many classes are children of MyAbstract, this may lead to a lot of work and code duplication.

In your case, where the method in question is declared by the interface which MyAbstract implements, you can actually just drop GetData completely and rely on the declaration of this function in ITest.

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

Comments

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.