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);
    }
}
i = 3
Compilation fails.
i = 5
A ClassCastException will occur.
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
27 comments Page 2 of 3.

Hari said:   1 decade ago
This is an example of inner class. To instantiate an inner class, we have to Instantiate outer class first.

Oshin said:   1 decade ago
Some time when we typecast superclass object into sub class it will throw a classCastException. When that Exception aries?

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.

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.

Ranjeet Kumar Sahu said:   1 decade ago
As we know Object class is the super class of the class hierarchy so the reference of Object class can refer to any of its sub class.

Again we are converting the Object reference to the foo object by typecasting that is possible.

Vinod said:   1 decade ago
We can access the class member within the class with object reference or we can access with class reference means with class name also.

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.

Kavita jalalli said:   1 decade ago
Object o = (Object)new Foo();
Foo foo = (Foo)o;

It is equal to
Foo foo=new Foo();

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.

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.


Post your comments here:

Your comments will be displayed after verification.