Inheritance Interface Interview Questions C#.NET

Inheritance Interface Interview Questions and Answers

In this article, I am going to discuss most frequently asked Inheritance Interface Interview Questions and Answers on C#.NET. To understand the below questions and answers first have a look at the below two articles where we discussed the Inheritance and Interface in details.

The Inheritance in C#

The Interface in C#

What are the 4 pillars of any object-oriented programming language?
  1. Abstraction
  2. Inheritance
  3. Encapsulation
  4. Polymorphism

Do structs support inheritance?

No, structs do not support inheritance, but they can implement interfaces.

What is the main advantage of using inheritance? 

Code reuse

Is the following code legal?
class ChildClass : ParentClassA, ParentClassB

No, a child class can have only one base class. We cannot specify 2 base classes at the same time. C# supports single class inheritance only. Therefore, we can specify only one base class to inherit from. However, it does allow multiple interface inheritance.

Does C# support multiple class inheritance?

No, C# supports single class inheritance only. However, classes can implement multiple interfaces at the same time.

Why does C# not support multiple class inheritance?

C# does not support multiple class inheritance because of the diamond problem that is associated, with multiple class inheritance. Let us understand the diamond problem of multiple class inheritance with an example.

Inheritance Interface Interview Questions and Answers on C#.NET

As shown in the image above:

1. I have 2 classes – ClassB and ClassC

2. Both of these classes inherit from ClassA

3. Now, we have another class, ClassD which inherits from both ClassB and ClassC

So if a method in ClassD calls a method defined in ClassA and ClassD has not overridden the invoked method. But both ClassB and ClassC have overridden the same method differently. Now, the ambiguity is, from which class does, ClassD inherits the invoked method: ClassB, or ClassC?

In order not to have these problems, C# does not support multiple class inheritance.

What is the difference between interfaces and abstract classes?

There are several differences between an abstract class and an interface as listed below.

  1. Abstract classes can have implementations for some of its members, but the interface can’t have the implementation for any of its members.
  2. Interfaces cannot have fields whereas an abstract class can have fields.
  3. An interface can inherit from another interface only and cannot inherit from an abstract class whereas an abstract class can inherit from another abstract class or another interface.
  4. A class can inherit from multiple interfaces at the same time, whereas a class cannot inherit from multiple abstract classes at the same time.
  5. Abstract class members can have access modifiers whereas interface members cannot have access modifiers as they are by default public.
When do you choose interface over an abstract class or vice versa?

If we have an implementation that will be the same for all the derived classes, then it is better to go for an abstract class instead of an interface. So, when we have an interface, we can move our implementation to any class that implements the interface. Whereas, when we have an abstract class, we can share implementation for all derived classes in one central place, and avoid code duplication in derived classes.

What are the advantages of using interfaces?

This is the most commonly asked interview question. This interview question is being asked in almost all the dot net interviews. It is very important that we understand all the concepts of interfaces and abstract classes.

Interfaces are very powerful. If properly used, interfaces provide all the advantages as listed below. 
  1. Interfaces allow us to implement polymorphic behavior. Of course, abstract classes can also be used to implement polymorphic behavior.
  2. The Interfaces allow us to develop very loosely coupled systems.
  3. Interfaces enable mocking for better unit testing.
  4. The Interfaces enable us to implement multiple inheritances in C#.
  5. Interfaces are great for implementing Inversion of Control or Dependency Injection.
  6. The Interfaces enable parallel application development.
Can an Interface contain fields?

No, an Interface cannot contain fields

What is the difference between class inheritance and interface inheritance?

Classes and structs can inherit from interfaces just like how classes can inherit a base class or struct. However, there are 2 differences.

A class or a struct can inherit from more than one interface at the same time whereas a class or a struct cannot inherit from more than one class at the same time

When a class or struct inherits an interface, it inherits only the method names and signatures, because the interface itself contains no implementations.

Can an interface inherit from another interface?

Yes, an interface can inherit from another interface. It is possible for a class to inherit an interface multiple times, through base classes or interfaces it inherits. In this case, the class can only implement the interface one time, if it is declared as part of the new class. If the inherited interface is not declared as part of the new class, its implementation is provided by the base class that declared it. It is possible for a base class to implement interface members using virtual members; in that case, the class inheriting the interface can change the interface behavior by overriding the virtual members.

Can you create an instance of an interface?

No, we cannot create an instance of an interface.

If a class inherits an interface, what are the 2 options available for that class?

Option1: Provide Implementation for all the members, inherited from the interface.

Option2: If the class does not wish to provide Implementation for all the members inherited from the interface, then the class has to be marked as abstract.

What do you mean by “Explicitly Implementing an Interface”? Give an example?

If a class is implementing the inherited interface member by prefixing the name of the interface, then the class is “Explicitly Implementing an Interface member”. The disadvantage of Explicitly Implementing an Interface member is that the class object has to be typecasted to the interface type to invoke the interface member. An example is shown below.

namespace Interfaces
    interface Car
        void Drive();

    class Demo : Car
        // Explicit implementation of an interface member
        void Car.Drive()
            Console.WriteLine("Drive Car");
        static void Main()
            Demo DemoObject = new Demo();

            // Error: Cannot call explicitly implemented interface method
            // using the class object.
            // Type cast the demo object to interface type Car
When to use Interface: 

If your child classes should implement a certain group of methods/functionalities but each of the child classes is free to provide its own implementation then use interfaces.

To learn more about inheritance and Interface please read the below articles. 

The Inheritance in C#

The Interface in C#


In this article, I try to explain most frequently asked Inheritance Interface Interview Questions and Answers on C#.NET. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *