Skip to content

Why is there a need to re-declare a method as "abstract" in an abstract class inheriting an interface

Sherif Ahmed

Asked Mar 31, 2009, 10:15 AM EDT5 answers
Share
LinkedInX
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 Bejaoui
    0
    Do you mean something like this if not rectify the under code to suit your special case

     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();
            }
           
        }
    0
  • 0
    Thanks for the reply. My question is simpler. Here it goes in words rather than code. In C# an abstract class that inherits from an interface must re-declare methods from the base interface that it does not implement as Abstract Methods. Why ?
    0
  • 0
    The interface has no implementation of those particular members. So either you can make those members abstract in an abstract class and pass them along without implementation, or you can implement them in the abstract class.
    0
  • 0
    I realize I can declare the method abstract in the abstract class and then implement in an inheriting class. (or implement in the abstract class) My question is why do i Need to declare the method as abstract in the abstract class to begin with ? Why does the language force me to declare the method as abstract in the inheriting Abstract class.
    0
  • 0
    It doesnt, if you wanted to you could give it an implementation in the abstract class. But since the class is abstract, if you don't want to give it an implementation you still have to declare it as abstract... its just one of those things with the C# language.
    0