Get benefited from interview Q/A and Online Test section. Subscribe to this blog to get updates directly in your mail box.
Best View in Google Crome, Mozilla firefox or IE 7 or above
Showing posts with label Default Constructor. Show all posts
Showing posts with label Default Constructor. Show all posts

Wednesday, March 30, 2011

Interview Question on Inheritance and Default Constructor

Here is a question on OOPS concepts like Inheritance and Default Constructor. The code provided is in C#.

There are three classes named A, B and C.
Class A has a default constructor that writes "In A" to console. Similarly Class B and C has default constructors that write "In B", and "In C" to Console respectively.
Class C inherits from Class B and Class B inherits from class A.

[Code]
class A
{
public A()
{
Console.WriteLine("In A");
}
}

class B:A
{
public B()
{
Console.WriteLine("In B");
}
}

class C:B
{
public C()
{
Console.WriteLine("In C");
}
}

[/Code]

Now, the question is as below.
If we instanciate class C, what will be the output.

[Code]
static void Main(string[] args)
{
C objC = new C();
Console.Read();
}

[/Code]

Result:
The result will be:
In A
In B
In C

  © Blogger templates Shiny by Ourblogtemplates.com 2008

Back to TOP