Why is there a need to re-declare a method as "abstract" in an abstract class inheriting an interface
I have a basic C# question that I thought I’d seek your help on.
In Java the following class structure is legitimate (and makes logical sense as AddInterfaceImpl is an abstract class and should not be obligated to implement all methods from the Interface it implements).
However the equivalent C# code throws error indicating that AddInterfaceImpl does not implement all the methods of the underlying interface).
C# forces me to declare GetString() as an abstract method in AddInterfaceImpl to avoid compiler errors.
Does someone have a logical reason as to why GetString() has to be re-declared as Abstract in the AddInterfaceImpl class ?
Java Code (Legal)
public interface AnInterface {
public String GetString();
}
public abstract class AnInterfaceImpl implements AnInterface {
public AnInterfaceImpl() {
GetString();
}
}
C# Equivalent
interface AnInterface
{
string GetString();
}
//Will Throw compilation error
// GetString has to be declared in AbstractImpl as string abstract GetString() in order to avoid compiler errors
public abstract class AbstractImpl : AnInterface
{
public AbstractImpl()
{
GetString();
}
}
5 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Bechir BejaouiRank #144 · 15.9K pointsPosted Mar 31, 2009, 9:30 PM EDT
interface inter
{
string GetString();
}
abstract class Class : inter
{
public string GetString()
{
return "Hello how are you : ) ";
}
}
class DerivedOne : Class
{}
class Program
{
static void Main(string[] args)
{
DerivedOne d = new DerivedOne();
string s = d.GetString();
Console.WriteLine(s);
Console.ReadLine();
}
}
Sherif Ahmed4 pointsPosted Apr 1, 2009, 2:22 AM EDT
Former memberPosted Apr 1, 2009, 12:54 PM EDT
Sherif Ahmed4 pointsPosted Apr 1, 2009, 3:46 PM EDT
Former memberPosted Apr 1, 2009, 7:26 PM EDT