Interview Questions Related to Inheritance
.NET supports only single inheritance. But multiple inheritance can be achieved using multiple interfaces.
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.:
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 |
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?
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.
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.
Very good information. Also the explanation it is very clear. Thanks for the post.
ReplyDelete@mauricio : Thanks mauricio.
DeleteKeep watching this blog. I have explained concepts topic wise.
Clearly explained. Thank you
ReplyDeleteThanks @Trupti Satardekar, you can follow us on google+ or subscribe to get updates
Delete