11

I have written the following code in C#.NET

public interface IWork
{
    void func();

}
public abstract  class WorkClass
{
    public void func()
    {
        Console.WriteLine("Calling Abstract Class Function");
    }

}

public class MyClass:WorkClass,IWork
{

}

On compiling, I didn't get any error. Compiler is not forcing me to implement the method "func();" in "MyClass", which has been derived from the interface "IWork".Latter, I can gracefully create a instance of the class "MyClass" and call the function "func()". Why the compiler is not forcing me to implement the "func()" method in the "MyClass"(which has been derived from "IWork" interface? Is it a flaw in C#?

5
  • 1
    Because the class MyClass inherits this functions from the abstract class WorkClass- therefore it is implemented. Commented Jul 6, 2012 at 8:55
  • If you want to be forced, lose it in your base class or lose it in your interface and mark it abstract in your base class. Commented Jul 6, 2012 at 9:01
  • Just a side note: There's no such thing as "multiple inheritance" in C#. You can only inherit from 1 class but implement many interfaces. Commented Jul 6, 2012 at 9:03
  • 1
    This is actually not even a bad question. I had a long time until I fully understood the idea behind interfaces...interfaces are actually only needed if there is no multiple inheritance. Otherwise you just create abstract classes as (implemented) interfaces and inherite those. This is the programmer's paradise. ;) Commented Feb 22, 2013 at 21:02
  • An interface is merely a contract, or a promise if you will. I give you an object of IDog and with that I promise it will be able to Poo(). Whether class Dog:Animal,IAnimal implements it or not is not an issue when class Animal implements it. The promise is met. Commented May 2, 2017 at 12:05

7 Answers 7

15

While reading about this subject, I found I couldn't easily put it all together in my head, so I wrote the following piece of code, which acts as a cheat-sheet for how C# works. Hope it helps someone.

public interface IMyInterface
{
    void FunctionA();
    void FunctionB();
    void FunctionC();
}

public abstract class MyAbstractClass : IMyInterface
{
    public void FunctionA()
    {
        Console.WriteLine( "FunctionA() implemented in abstract class. Cannot be overridden in concrete class." );
    }

    public virtual void FunctionB()
    {
        Console.WriteLine( "FunctionB() implemented in abstract class. Can be overridden in concrete class." );
    }

    public abstract void FunctionC();
}

public class MyConcreteClass : MyAbstractClass, IMyInterface
{
    public override void FunctionB()
    {
        base.FunctionB();
        Console.WriteLine( "FunctionB() implemented in abstract class but optionally overridden in concrete class." );
    }

    public override void FunctionC()
    {
        Console.WriteLine( "FunctionC() must be implemented in concrete class because abstract class provides no implementation." );
    }
}

class Program
{
    static void Main( string[] args )
    {
        IMyInterface foo = new MyConcreteClass();
        foo.FunctionA();
        foo.FunctionB();
        foo.FunctionC();
        Console.ReadKey();
    }
}

Gives the following output:

FunctionA() implemented in abstract class. Cannot be overridden in concrete class.

FunctionB() implemented in abstract class. Can be overridden in concrete class.

FunctionB() implemented in abstract class but optionally overridden in concrete class.

FunctionC() must be implemented in concrete class because abstract class provides no implementation.

Sign up to request clarification or add additional context in comments.

Comments

5

To better understand the concept behind interfaces, I just give you the correct code of your implementation:

public interface IWork{
    void func();
}

public abstract class WorkClass,IWork{
    public void func(){
        Console.WriteLine("Calling Abstract Class Function");
    }
}

public class MyClass:WorkClass{
...
}

The basic rule: You need to include the interface always where the implementation is. So if you create a method within an abstract classes and define an interface of this method, you'll need to implement the interface into your abstract class and then all subclasses will automatically implement this interface.

As a matter of fact, interfaces have 2 kind of functions you can use them for:

1) As a "real" interface providing a generic handling of any class implementing the interface, so you can handle several kind of classes just by one interface (without knowing their real class names). While "handling" means: Calling a method.

2) As a help for other (framework) programmers not to mess up with your code. If you want to be sure that an method won't be replaced with another name, you define an interface for your class containing all "must have" method names. Even if the method is called nowhere, your programmer will get an compile error message when he changed the method name.

Now you can easily handle your Myclass just by the interface IWork.

Comments

3

Because the abstract class implements the interface.

If your class MyClass would not inherit from WorkClass you would get an error saying 'MyClass' does not implement interface member 'IWork.func()'.

But you also inherit from WorkClass, which actually implements the methods that the interface requires.

You can mark func() as abstract if you want to force the classes that inherits from it to implement it like this:

public abstract class WorkClass
{
    public abstract void func();

}

Comments

1

I tried with the classes above in my solution. It inherits the abstract class so the derived class have the func() method definition. This is the reason it was not able to show compiled errors.

Comments

0

func() is not marked as abstract in the WorkClass so you don't need to implement it in any classes that derive from WorkClass.

WorkClass implements the IWork interface so you don't need to implement it in MyClass because it inherits func() from WorkClass.

Comments

0

Since the func method in the abstract class is a non-virtual method, so the compiler thinks this method is a implementation of the interface.

Comments

0

When you extend MyClass to WorkClass, the method func() (which has been defined), is inherited.

So, when the interface IWork is implemented, the method 'func()' has already been defined. So, there are no more undefined methods in MyClass.

So, the class MyClass is a concrete class, due to which you are able to create a MyClass object without any compilation errors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.