Question

As we all know that an interface can inherit another interface, and interface can only contain method signature.

interface A
{
    void DoWorkA()
    { }
}

interface B : A
{
    void DoWorkB()
    { }
}

Now the class which implement interface B need to provide body of two functions.

public class ABC : B
{
    public void DoWorkB()
    {
        //do some work
    }

    public void DoWorkA()
    {
        //do some work
    }
 }

So why can't be a interface can inherit a abstract class or a normal class.

So if interface inherited abstract class then class which implement this interface need to provide implementation of all interface and abstract methods of abstract class.

Why this is not possible.

Was it helpful?

Solution

The major difference between interfaces and classes is that interfaces only provide signatures and not implementations. If the interface inheriting a class inherited its implementation, it would provide implementation and no longer be an interface.

OTHER TIPS

Because interface is a contract by language definition. It's a language specific type for describing a contract: so you can aggregate different contracts (inherit one interface from another), but contract by itself can not extend something.

Abstract class, can contain implementation:

//PERFECTLY VALID ABSTRACT CLASS DEFINITION
public abstract class A {

    public abstract void Overridable(); //ABSRTACT

    public void DoSomething() {
       //METHOD BODY
    }
}

So inherit interface from something that may, and often, has implementation, does not make sense from language design and OPP point of view.

Because an interface is not allowed to have any methods or constructors defined in it by definition.

Inheriting an interface from a class would mean that it has some methods and at least a constructor. Even if you do not define a constructor in the base class the compiler would still generate one. The interface would also need a constructor to call the base constructor so you would be violating the afore mentioned rule.

Depending on the case you can be violating much more rules. If the base class has some methods or field the interface would inherit it too.

What happens next if you inherit a class from an interface which inherits another class? You can have multiple inheritance if you inherit multiple interfaces in a class which is prohibited in all .net languages.

  1. AS per my knowledge if you are inheriting the abstract class in interfaces(which you cannot do it actually)then all the abstract methods in the abstract class has to be overridden .But you cannot provide the functionality(defining) for the methods in interfaces this is one reason.

  2. Another reason for not inheriting normal classes in interfaces is ,if you are inheriting any class in the sense taking on the properties of existing objects(interface is all the members(even the functionalities) of the existing class) which is not supported in interfaces.

  3. You cannot create a constructor or destructor in interfaces but existing class contains minimum one constructor(if not compiler will create it).

  4. If the existing class inherits any another class then the interface is inheriting two sub class which is not supported in c#(which may contain the same method names).

You will have many more reasons for supporting inheritance in interfaces.

Theoretically you could design a language in which this was possible. The designer of C# choose instead to follow the common convention of OOP in which class do not imply interfaces and therefore cannot be inherited as such.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top