@Blog.Author(Nandip Makwana) .LearningExperience(ASP.NET, ASP.NET MVC, IIS, jQuery & Technology Surrounding it...)

June 2, 2012 comment ,

Does .NET support multiple inheritances?

Yesterday I was asked this very basic question and as it is very much clear that .NET do not support multiple inheritances so I answered no it is not possible. But many people (including reader of this blog and my tech friends) believe that we can achieve multiple inheritances with the help of interface and I was always shocked in past also when someone said that we can do multiple inheritances with interface. So let see why it is not multiple inheritances.

Interface has nothing which can be inherited

As we know that interface does not contain concrete implementation of its member. So it has nothing which can be inherited by other i.e. class. Interface just holds the member definition or signature which must be implemented by implementing class.

I think above three line paragraph is enough to explain why it is not inheritance when we implement interface. So now question is that why many developers believe that it is multiple inheritances? I think they are misguided due to common syntax of inheritance and interface implementation in c#.

Expert sentence: Interface has nothing which can be inherited by other so it is not inheritance when we implement interface.

Hope this basic post would be helpful.

UPDATE: 4-Jun-12

After reading this post, reader of this blog and few of my friends told me that it is inheritance when we implement interface or inherit one interface from another interface. So let me explain it more clearly with code example that why it is not inheritance. Have a look at following code snippet.

public interface ITestInterface
{
    void TestInterfaceMethod();
}
public class BaseClass
{
    public void BaseClassMethod()
    {}
}
public class ChildClass : BaseClass, ITestInterface
{
    public void ChildClassMethod()
    {}
    public void TestInterfaceMethod()
    {throw new NotImplementedException();}
}
 
ChildClass obj = new ChildClass();
obj.ChildClassMethod();
obj.BaseClassMethod();
obj.TestInterfaceMethod();

Now compile above code and open Object Browser (look at below image.) We can access all three methods with above code. But in Object Browser it is not displaying BaseClassMethod under ChildClass because it is inherited from BaseClass. While it is displaying TestInterfaceMethod under ChildClass because it is not inherited but enforced by ITestInterface that such method is required in order to work with ITestIterface. This is also same when we implement interface which is inherited from another interface.

Let me know your view on above.

comments powered by Disqus

Featured Content

Resources & Tools

About Nandip Makwana

Nandip Makwana is passionate about digital world and web. He completed his Masters in Computer Application in June 2011. Currently he is working as a Software Engineer. He has shown great promise and command over ASP.NET and technologies surrounding it during his academic years and professorial life...continue reading