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!
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!
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
.
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