Back to: Dot Net Interview Questions and Answers
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 and Answers in C#. Please read our previous article where we discussed the most frequently asked 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.
- What are the 4 pillars of any object-oriented programming language?
- Do structs support inheritance in C#?
- What is the main advantage of using inheritance in C#?
- Does C# support multiple class inheritance?
- Why does C# not support multiple class inheritance?
- What are the differences between interfaces and abstract classes?
- When do you choose interface over an abstract class or vice versa?
- What are the advantages of using interfaces?
- Can an Interface contain fields?
- What is the difference between class inheritance and interface inheritance?
- Can an interface inherit from another interface?
- Can you create an instance of an interface?
- What do you mean by “Explicitly Implementing an Interface”? Give an example?
- When to use Interface?
What are the 4 pillars of any object-oriented programming language?
- Abstraction
- Inheritance
- Encapsulation
- 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.
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.
- Abstract classes can have implementations for some of their members, but the interface can’t have the implementation for any of its members.
- Interfaces cannot have fields where an abstract class can have fields.
- 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.
- A class can inherit from multiple interfaces at the same time, whereas a class cannot inherit from multiple abstract classes at the same time.
- Abstract class members can have access modifiers where as 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 (function with the body) that will be the same for all the derived classes, then it is better to go for an abstract class instead of an interface. 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.
- Interfaces allow us to implement polymorphic behavior. Of course, abstract classes can also be used to implement polymorphic behavior.
- The Interfaces allow us to develop very loosely coupled systems.
- Interfaces enable mocking for better unit testing.
- The Interfaces enable us to implement multiple inheritances in C#.
- Interfaces are great for implementing Inversion of Control or Dependency Injection.
- 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 the 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.
The answer for question above i.e. When do you choose interface over an abstract class or vice versa? is not clearly explained. Looks like there are some typos. It only says about abstract class. There is no mention or explanation about interface. This question been asked in almost all interviews. Can you please provide correct answer. Thanks.
* Interface does not have fields, when we want common fields among all the derived classes then go for the abstract class.
* An abstract class can have constructor, this is one major difference between an abstract class and an interface. With the help of the constructor we can also initailized the fields of abstract class.
* When we have some level of common functionality that must be implemented by derived classes then abstract classes will be better.
* We should use an interface if we want a contract on some behavior or functionality. Interface shoud not be used if the same code need to write for the interface methods in this case, an abstract class will be better, define the method once, and reuse it as needed.
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. This point is not working.
Why it’s not working ? can you give us some examples ?
That’s correct. Because if you mark the class as abstract you cannot create an instance of it directly, so you need to create a derived class and implement the method in order to use the first class what doesn’t make sense.
If you are inheriting from an interface you need to provide an implementation for all methods at some point.