.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

why this output??

Posted By: susant     Posted Date: October 30, 2008    Points:2   Category :C#
public class shpe2D
{
public shpe2D() //default constructor
{
Console.WriteLine("Hi");
}
}
public class shape3D : shpe2D //Inherits from shape2D class
{
public shape3D() // Default constructor
{
Console.WriteLine("Hello");
}
}
static void Main(string[] args)
{
shape3D obj = new shape3D(34); //Object creation of Shape3D class
Console.Read();
}


OUTPUT

Hi
Hello

confusion:
If i am creating the object of shape3D class the OUTPUT should be "Hello" Why its calling base class constructor??


Susant
.Net

Responses
Author: Pankaj Mishra             
Posted Date: October 30, 2008     Points: 5   

Hi Sushant,

In Inheritance Base classes are automatically instantiated before derived classes.

In you above example you wrote "Hi" inside the base class contructor and when you say this line

shape3D obj = new shape3D();

"shpe2D" are automatically instantiated before "shape3D" classes.

Hope this will help you

Cheers
Pankaj

Author: Savi3             
Posted Date: October 30, 2008     Points: 5   

Hi Susant,

u create the obj for shape 3D first.

That Shape 3D is also derived from shape 2d

In shape2d first first constructor only so it only execute first



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend