Java Programming - Declarations and Access Control - Discussion
Discussion Forum : Declarations and Access Control - Finding the output (Q.No. 2)
2.
What will be the output of the program?
public class Test
{
public static void main(String args[])
{
class Foo
{
public int i = 3;
}
Object o = (Object)new Foo();
Foo foo = (Foo)o;
System.out.println("i = " + foo.i);
}
}
Discussion:
27 comments Page 1 of 3.
Mukta said:
1 decade ago
It will work when simply written
Foo foo = new Foo();
also.
no need to write any other statement except this.
but in Question Object class is used.which is superclass of all classes direct or indirect in java including your class also.so in other words father of all classes.so it can store any reference of any object.
here its the same
Object o = (Object)new Foo();
once the Foo object reference is stored in o ref variable.it is then typecasted in Foo class type.this is needed because we want to access int i variable of foo class.we can not call it directly by using o reference.it will give error cannot find symbol.
Foo foo = new Foo();
also.
no need to write any other statement except this.
but in Question Object class is used.which is superclass of all classes direct or indirect in java including your class also.so in other words father of all classes.so it can store any reference of any object.
here its the same
Object o = (Object)new Foo();
once the Foo object reference is stored in o ref variable.it is then typecasted in Foo class type.this is needed because we want to access int i variable of foo class.we can not call it directly by using o reference.it will give error cannot find symbol.
Sunil said:
1 decade ago
As object is the superclass for all the defined classes so, in the line.
Object o = (Object)new Foo();
We can assign the downcasting to the same class it doesn't be worried about,it can be done for any class.Now in the next line.
Foo foo = (Foo)o;
There is a downcasting and this kind of casting is legal in java.So it is legal and as o is defined after Foo it stands for the following code.
(Object)new Foo();
So finally there will be a expression as below,
Foo foo=(Foo)(Object)new Foo();
It is valid and properly legal in java code.
Object o = (Object)new Foo();
We can assign the downcasting to the same class it doesn't be worried about,it can be done for any class.Now in the next line.
Foo foo = (Foo)o;
There is a downcasting and this kind of casting is legal in java.So it is legal and as o is defined after Foo it stands for the following code.
(Object)new Foo();
So finally there will be a expression as below,
Foo foo=(Foo)(Object)new Foo();
It is valid and properly legal in java code.
Asim Husain said:
1 decade ago
i=3 is correct answer.There is no need of type casting here.Type casting is needed when we subclass has reference of superclass.but we see here we are assigining instance of subclass as
Object o = (Object)new Foo();//we are assignigning instance
type casting will be required as the following condition.
Object o="";
Foo f=new Foo();
f=(Foo)o;
System.out.println("i="+f.i);
Type casting exception will occur in this case.
Object o = (Object)new Foo();//we are assignigning instance
type casting will be required as the following condition.
Object o="";
Foo f=new Foo();
f=(Foo)o;
System.out.println("i="+f.i);
Type casting exception will occur in this case.
Zin said:
9 years ago
It's using the simple concept of Casting. Here two things are happening. We are explicitly up casting foo object t o object class type reference. And in the next one we are trying to downcast object type instance of an object into custom (user-created class foo). Upcasting is automatic. Don't require to upcast any child class to object class.
But downcasting is mandatory. If you avoid explicit downcasting here, you will get an error.
But downcasting is mandatory. If you avoid explicit downcasting here, you will get an error.
(1)
Vivek said:
1 decade ago
Object o = (Object)new Foo();
Above line means that the reference variable of Object(class which is the super class of all the classes) is holding the object of sub class Foo. And cast applied to it is optional.
Foo foo = (Foo)o;
Means we are extracting the object of Foo class from the reference of Object class which is o. Now, foo.i is valid and can be printed as 3.
Above line means that the reference variable of Object(class which is the super class of all the classes) is holding the object of sub class Foo. And cast applied to it is optional.
Foo foo = (Foo)o;
Means we are extracting the object of Foo class from the reference of Object class which is o. Now, foo.i is valid and can be printed as 3.
Kripanshu said:
1 decade ago
Object is the super class of all the classes so we can put any instance's reference in the Object reference.
So first reference of new Foo() i.e new object of Foo is given in Object reference o;
Now Object reference o is type cast into Foo stored in foo reference;
Now simply accessed the public data member i of class Foo outside the class using (.) operator.
So first reference of new Foo() i.e new object of Foo is given in Object reference o;
Now Object reference o is type cast into Foo stored in foo reference;
Now simply accessed the public data member i of class Foo outside the class using (.) operator.
Gurwinder Singh said:
1 decade ago
Here dmd technique is used and no need of casting it. Again that reference address is copied to same inner class reference. That completed the consturction of inner class. So it gave the outpur i=3;.
Note: When reference of Object class is copied to inner class. Type casting is required.
Note: When reference of Object class is copied to inner class. Type casting is required.
Sumeet sharma said:
1 decade ago
1. Declaration of new type inside any method is valid.
2. Since new Object of Foo was casted into Object, it's valid.
3. Since Object O contains "Foo" type of object hence casting from object to Foo is valid.
4. At last we are accessing public member of Foo, which is also valid.
2. Since new Object of Foo was casted into Object, it's valid.
3. Since Object O contains "Foo" type of object hence casting from object to Foo is valid.
4. At last we are accessing public member of Foo, which is also valid.
Rishabh Rastogi said:
1 decade ago
Hi friends! Yes, the output will be i=3 i.e. option A is correct.
One more thing.
If we simply write,
/*
* Object o = (Object)new Foo();
* Foo foo = (Foo)o;
*/
Foo foo = new Foo();
System.out.println("i = " + foo.i);
Then also the output is i=3.
One more thing.
If we simply write,
/*
* Object o = (Object)new Foo();
* Foo foo = (Foo)o;
*/
Foo foo = new Foo();
System.out.println("i = " + foo.i);
Then also the output is i=3.
Aniket said:
1 decade ago
Object o=(Object)new Foo();-->Here object of Class Foo is Created and Converted it into Super Class Object.
Foo foo=(Foo)o;-->Here the object is Converted into Subclass i.e. Foo and Assign to reference foo.
And finally the Value of i=3 is displayed by foo.i
Foo foo=(Foo)o;-->Here the object is Converted into Subclass i.e. Foo and Assign to reference foo.
And finally the Value of i=3 is displayed by foo.i
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers