aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class.." Watch "Inner class.." New topic
Author

Inner class..

Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
hi, i got it from whizlabs practice exam3.

class Myouter
{
private class Myinner
{
float f()
{
return 1.3;
}
}
Myinner getIt()
{
return new Myinner();
}
}

class Outside
{
public static void main(String args[])
{
Myouter outer=new Myouter();
System.out.println(outer.getIt().f());
}
}

Answer oes not compile since the f() returns double value. This is ok. Thay have given additional information on inner class as following.

Myinner is a private class,so you cannot access it directly from ouside code, for eg, new Myouter().new Myinner().f(); will give compile error.
but in the given program we get the inner class object reference using a method of the outer enclosing class,which is allowed. since the f() is ot private you can access it using obtained reference.

Now.. i tried this program with correct return value of f().but it gives error that f() in Myouter.Myinner is defined in an inaccessible class or interface.
System.out.println(outer.getIt().f());//error.

please someone clear this. if the above statement is true, why this error?



Thanks in advance
Preetha
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

We can get the reference of the private class by using getIt() but cant invoke any of its method as its members and methods are not visible. The private class hides their visibility.


Not sure but I think this is the reason. Someone please verify.


My Blog SCJP 5 SCWCD 5
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Yes you are right Himanshu, but what is the use of such reference ?


SCJP 6
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

I think the use is to confuse some innocent exam takers.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Hmmm good use
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
please read this..
In the given program we get the inner class object reference using a method of the outer enclosing class,which is allowed. since the f() is not private you can access it using obtained reference.

can we access f() which is in private inner class through the reference obtained from the outer class method getIt()?

Thanks
Preetha
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Compiler is saying no, so the given statement is wrong here. May be somebody experienced will tell us more about this.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9313
    
  17

If you compile the original program, it gives two errors on my JDK 1.6. One for the float and one for accessing the private inner class. You cannot use the method of the inner class the way you are using it. You can however use a base class for the inner class and use the method using a polymorphic call



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Preethi Dev
Ranch Hand

Joined: Sep 07, 2008
Posts: 265
Thanks Ankit,
I got it after reading your blog. really good stuff

Preetha
 
 
subject: Inner class..