The blog has moved to the new site F10Debug.com

Sunday, May 3, 2015

Interview Questions on Inheritance in C#.Net

Interview Questions Related to Inheritance


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.:


Inheritance Hierarchy


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?

Diamond Problem 


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

Note: Guys please comment, if this blog help you. 
Also if you come across any question's which are not covered or ask somebody you in       interview, so please feel free to ask.

4 comments:

  1. Very good information. Also the explanation it is very clear. Thanks for the post.

    ReplyDelete
    Replies
    1. @mauricio : Thanks mauricio.

      Keep watching this blog. I have explained concepts topic wise.

      Delete
  2. Replies
    1. Thanks @Trupti Satardekar, you can follow us on google+ or subscribe to get updates

      Delete