1
Answer

Abstract Class in C#

Sherif Ahmed

Sherif Ahmed

15y
3.9k
1

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();

      }

}

Answers (1)