Click here to Skip to main content
15,998,100 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is it possible "implement interface method in Abstract class"?

Like

Interface I1
{

int Add(int i1, int i2);



}
Public abstract class abstClass : I1
{

public Abstract int Add(int a, int b)
{
return a+b;
}
{

}
}

class childCls
{
//How can i call those here ?
}
Posted
Comments
CPallini 5-Aug-13 3:51am    
Yes, it is possible (see OriginalGriff's code). However (as he correctly pointed out) you cannot declare the method abstract.

1 solution

You can't - you cannot declare a method as abstract and then provide a body. You can perfectly happily say:
C#
interface I1
    {
    int Add(int i1, int i2);
    }
public abstract class abstClass : I1
    {
    public int Add(int a, int b)
        {
        return a + b;
        }
    }
public class myClass : abstClass { }
    ...
    myClass mc = new myClass();
    Console.WriteLine(mc.Add(1, 2));
Or
C#
interface I1
    {
    int Add(int i1, int i2);
    }
public abstract class abstClass : I1
    {
    public abstract int Add(int a, int b);
    }
public class myClass : abstClass
    {
    public override int Add(int a, int b)
        {
        return a + b;
        }
    }
    ...
    myClass mc = new myClass();
    Console.WriteLine(mc.Add(1, 2));
 
Share this answer
 
Comments
CPallini 5-Aug-13 3:49am    
5.
Pheonyx 5-Aug-13 3:57am    
Could you not use the "Virtual" keyword instead of "Abstract" on the method if you wanted the class to have come base implementation?
OriginalGriff 5-Aug-13 4:18am    
You could, but that's not what the question was asking. :laugh:

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