Inheritance and Interface Interview Questions in C#

Inheritance and Interface Interview Questions in C# with Answers

In this article, I am going to discuss the most frequently asked Inheritance and Interface Interview Questions in C# with Answers. Please read our previous article where we discussed the most frequently asked basic C# Interview Questions and Answers. As part of this article, we are going to discuss the following Inheritance and Interface Interview Questions in C# with answers.

  1. What are the 4 pillars of any object-oriented programming language?
  2. Do structs support inheritance in C#?
  3. What is the main advantage of using inheritance in C#?
  4. Does C# support multiple class inheritance?
  5. Why does C# not support multiple class inheritance?
  6. What are the differences between interfaces and abstract classes?
  7. When do you choose interface over an abstract class or vice versa?
  8. What are the advantages of using interfaces?
  9. Can an Interface contain fields?
  10. What is the difference between class inheritance and interface inheritance?
  11. Can an interface inherit from another interface?
  12. Can you create an instance of an interface?
  13. What do you mean by “Explicitly Implementing an Interface”? Give an example?
  14. When to use Interface?
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 and Interface Interview Questions in C# with Answers

As shown in the image above, I have 2 classes – Class B and Class C and Both of these classes are inherited from Class A. Now, we have another class i.e. Class D which is inherited from both Class B and Class C

So if a method in Class D calls a method defined in Class A and Class D has not overridden the invoked method. But both Class B and Class C have overridden the same method differently. Now, the ambiguity is, from which class does, Class D inherits the invoked method: Class B, or Class C?

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();

            //DemoObject.Drive();
            // Error: Cannot call explicitly implemented interface method
            // using the class object.
            // Type cast the demo object to interface type Car
            ((Car)DemoObject).Drive();
        }
    }
}
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.

In the next article, I am going to discuss the most frequently asked Abstract and Sealed Class Interview Questions with Answers. Here, in this article, I try to explain most frequently asked Inheritance and Interface Interview Questions in C# with Answers. I hope this article will help you with your needs. 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 *