Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
What happens when a class is initialized by another class like :
C#
class a
{
   
}
class b
{

}
class c
{
   static void Main(String aa[])
   {
    a obj=new b();
   }

}

thank u!!!
Posted
Updated 25-Mar-13 18:00pm
v2
Comments
bbirajdar 25-Mar-13 23:24pm    
Why dont you try compiling the code in Visual Studio instead ?? There are multiple issues with your code above

Once you create an instance of a class, it is kept in memory till the parent class execution ends.
This instance is then viable for garbage collection.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Mar-13 0:23am    
More detail, a 5. One note: the word "parent" in "parent class" is not very adequate here.
—SA
Abhinav S 27-Mar-13 6:04am    
Ok. Thanks.
Just think about it: what's the difference where the object is created? Method is method, no matter of what class. Think, just use some logic.

[EDIT]

You need to understand that two memory objects are involved: a reference itself and a referenced object. If the object becomes unreachable, it will be eventually garbage-collected:

http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^]

In your case, you won't be able to use the project you have created: you create it and forget the reference when the method is returned. So, it makes no practical sense.

—SA
 
Share this answer
 
v3
Comments
_Amy 26-Mar-13 0:01am    
Thinking is the best job. :) +5!
Sergey Alexandrovich Kryukov 26-Mar-13 0:13am    
Thank you, Amy.
It looks something else does not like it. :-)
—SA
_Amy 26-Mar-13 0:16am    
For me it's a part of motivation. :)
Abhinav S 26-Mar-13 0:18am    
Hoped to correct the downvote. 5.
Sergey Alexandrovich Kryukov 26-Mar-13 0:20am    
Thank you, Abhinav.
—SA
You cannot create an object of b with reference of A unless You declare A as parent class and B as child class. The below line will give you compile time error.

a obj=new b();


CSS
If you want to derive B class as reference of A,then declare
class B as child class
like this
class a
{

}
class b:A
{

}
 
Share this answer
 
v2
Comments
qqqqqqqqqqqqqqqqqqqaaaaaaaa 26-Mar-13 0:57am    
yes! i have to dirive the class A to class B..
Santhosh Kumar Jayaraman 26-Mar-13 0:58am    
Then declare
class B as child class
like this
class a
{

}
class b:A
{

}
qqqqqqqqqqqqqqqqqqqaaaaaaaa 26-Mar-13 1:01am    
I m new in programing. does this code
A obj=new B();
means create an object of B not A. I m not sure.please help !!!!
Santhosh Kumar Jayaraman 26-Mar-13 1:04am    
Yes it means you are creating object of B with reference to A. If you want to create object of B, you have to use B obj=new B();
qqqqqqqqqqqqqqqqqqqaaaaaaaa 26-Mar-13 1:07am    
thanks a lot!my concept is clear now about initialization of object , is there any ways of initialization..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900