Wednesday 23 March 2011

Inheritance Interview Questions

What is Inheritance?

Inheritanc eprovides reusability and extensibility features.
Reusability:  Allow us to use a a component,class and even methods in different applications.
Extensability: Means to extend a module as a new need arises.

Does C# support multiple class inheritance?
No, C# supports doesn't support multiple class inheritance.We can use interface in place of that.

Do structs support inheritance?
No, structs do not support inheritance, but they can implement interface.

Is the following code legal?
class Employee
{
//code for Employee class
}
class Department
{
//code for Department claSS
}
class Clerk : Employee, Department
{
}


NO, Because c# doesnot support multiple inheritance so one class cannot inherit more then one class. Though if Employee and Department are interfaces rather than class then it is possible.

What will be the output of the following code? (very important question asked in almost every interviews)
public class Bank
{
public Bank()
{
Console.WriteLine("I am a bank class");
}
}
public class account : Bank
{
public account()
{
Console.WriteLine("I am a account class");
}
static void Main()
{
account a = new account();
}

}
Output:
I am a bank class
I am a account class

Base class constructor is always called before the child class.


No comments:

Post a Comment