Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: (untagged)
Suppose i have Iadd() interface with method add().I have abstract class math:Iadd() and also i have one derived class derived() which inherites the abstract class.where shoud i write the add() method implementation ? In abstratc class or in derived class ?Please help me on this.

C#
Iadd()
{
add()
}

abstratc class add():Iadd()
{
//where i can implement the Add() method and how ?
}

class derived:add()
{
//where i can implement the Add() method and how?

}
Posted
Updated 31-Aug-15 3:42am
v2
Comments
PIEBALDconsult 31-Aug-15 9:41am    
You _may_ implement it in the abstract class if that makes sense for your code. Or you _may_ define it in the abstract class and implement it in the derived class(es).
Philippe Mori 31-Aug-15 12:14pm    
Why not try it? The compiler would tell you if it works!

1 solution

No, you cannot skip interface method implementation in any class which has this interface in its inheritance list. It does not matter if this class is abstract or not. If you think at this, perhaps you can understand why it is designed this way; in brief, it would defeat the purpose of interfaces.

At the same time, you can create an implementation of the interface method, represented by an abstract virtual method. This is a weird thing: from the standpoint of interface, this is an implementation of its method, but the method is abstract, so there is no an implementation. Anyway, it's more important to understand how it works and understand that this is quite a reasonable thing.

It would have reasonable purpose, you can later override the method. Practically, it may make sense if you, for example, can implement part of interface methods earlier, and part later. This is how it may look:
C#
interface ISample {
    void First
    void Second();
}

abstract class Base : ISample {
    public void First() {
        // do something,
        // or not, then such method
        // is sometimes called 'pseudo-abstract'
    }
    public abstract void Second(); // virtual abstract 
}

Or you can use explicit implementation which is often better, because it would allow to call interface members only through an interface reference and not through the implementing class instance:
C#
abstract class Base : ISample {
    void ISample.First() { }
    abstract void ISample.Second(); 
}

And then you can later fully implement ISample.Second in one of derived classes, and finally get some non-abstract classes which could be instantiated. Then you can assign the class instance to a variable of an interface type and call interface methods/properties.

—SA
 
Share this answer
 
v2
Comments
Maciej Los 31-Aug-15 10:37am    
Short and to the point, +5!
Sergey Alexandrovich Kryukov 31-Aug-15 10:40am    
Thank you, Maciej.
—SA
PIEBALDconsult 31-Aug-15 22:07pm    
"an abstract implementation"

There's no such thing as "an abstract implementation".
Sergey Alexandrovich Kryukov 31-Aug-15 22:49pm    
Yes, wrong words, I must admit. I'll think how to explain it more correctly. But, essentially, 1) from the standpoint of interface implementation, this is, formally, an implementation; 2) the function is abstract; 3) the technique is quite usual. Do you have an idea how to properly call it? I edited the answer.
Thank you.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900