Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple inheritance with Abstract class and Interface

Tags:

c#

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#?

like image 906
techmad Avatar asked Jul 06 '12 08:07

techmad


People also ask

Can we do multiple inheritance with abstract class?

Inheritance is another feature of object-oriented programming where a particular class can derive from a base class. Multiple inheritance allows the extension of more than one base class in a derived class. Abstract classes do not support multiple inheritance. Interfaces support multiple inheritance.

Can a class inherit both interface and abstract class?

An abstract class defines the identity of a class. An interface can inherit multiple interfaces but cannot inherit a class. An abstract class can inherit a class and multiple interfaces. An interface cannot declare constructors or destructors.

Can multiple inheritances support in interface?

As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.

How is inheritance incorporated by abstract class and interface?

Interfaces can also be considered an abstract class which group similar methods without any implementation. To use interface in the java code, 'implements' keyword is used. A class can implement several interfaces, thus providing similar features that are provided by multiple inheritance.


1 Answers

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.

like image 73
Patrick Avatar answered Sep 19 '22 15:09

Patrick