• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner class..

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm good use
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit,
I got it after reading your blog. really good stuff

Preetha
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic