Interview Questions on Inheritance in C#.Net

By | November 19, 2017

Interview Questions on Inheritance in C#.Net

1) What is inheritance hierarchy?

In Inheritance, The class which derives fundamental functionality from base class (Parent class) is called as derived class (Child class). This derived class can also be act like base class for another class. There for, it is possible to create tree-like structure that illustrates the relationship between all related classes. This structure is known as the inheritance hierarchy.
For E.g.:

What is inheritance hierarchy in .Net

What is inheritance hierarchy in .Net

In above example Class A is parent class for class B & C (Derived classes), Also class B is parent class for class D & E and also same time it is child class of class A. this is call as Inheritance Hierarchy.

2) Can we have multiple inheritances in .NET?

.NET supports only single inheritance. But multiple inheritance can be achieved using multiple interfaces.

3) Why don’t we have multiple inheritance in .NET?

Diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override it), and B and C have overridden that method differently, then via which class does it inherit: B, or C?

Why don’t we have multiple inheritance in .NET?

Why don’t we have multiple inheritance in .NET?

4) How do you prevent a class from inherited in C#.Net?

Sealed keyword is used to prevent class from being inherited.

Some important points related to Sealed Class as follows,

a)  Sealed class cannot be used as abstract class or base class.

// trying to use sealed class as abstract class

Sealed abstract Class SealedClass // error will occur, you can’t declare like this

 b)  You can create instance of sealed class to access its members & properties.

// Sealed class
Sealed Class SealedClass
{
   Public int a,
   Public string b
}
// you can create instance of above class as follows,
SealedClass Obj = new SealedClass();

c) You cannot derived a class form Sealed class

Class SealedClass: ClassA

 5) If we inherit the class does the private variables also inherited?

       Yes

 

Leave a Reply

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