Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class..

 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you are right Himanshu, but what is the use of such reference ?
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the use is to confuse some innocent exam takers.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm good use
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler is saying no, so the given statement is wrong here. May be somebody experienced will tell us more about this.
 
Ankit Garg
Sheriff
Posts: 9518
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ankit,
I got it after reading your blog. really good stuff

Preetha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic