0

Assuming X and Y both class.What does below statements means?

X obj1 = new X();
X obj2 = new Y();

Whose reference and whose memori will be given in above case? please elaborate!

Niyat patel
  • 23
  • 1
  • 7

2 Answers2

2

This creates an object of type X and assigns a reference to it to a variable called obj1.

It then creates another object, this time of of type Y, and assigns a reference to it to a variable called obj2.

For the second line to compile, Y has to be a subclass of X.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • I still didn't get you...What does object of type Y means in sense of memory and reference? – Niyat patel Oct 13 '14 at 18:35
  • @Niyatpatel: Does this help http://stackoverflow.com/questions/9224517/what-is-a-class-reference-and-an-object? – NPE Oct 13 '14 at 18:37
0

X is just a handler. The actual object creation is defined by the new statement. So

X obj1 = new X();  // ---> Will create an X() object 
X obj2 = new Y();  // ---> Will create an Y() object 

In each case they reference a separate location in the memory. In other words, obj1 != obj2

Mohammad Najar
  • 2,009
  • 2
  • 21
  • 31