• 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

Private inner class question

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Private inner class&interface question... help~



Q:

At place //1 , the compiler says that I need to have an instance of PrivateInnerClassTest (outer class) to instantiate the inner class, like outer.new InnerClass(). But I am inside the outer class, and should not need that outer.new, but just new, right?

At place //2, the compiler says that I reduced the visibility of method fun when overriding. But as far as I can see, they both have default accessibility, not less restricted. So what is the problem then?

Thanks a lot!
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yea Mua wrote:
At place //1 , the compiler says that I need to have an instance of PrivateInnerClassTest (outer class) to instantiate the inner class, like outer.new InnerClass(). But I am inside the outer class, and should not need that outer.new, but just new, right?


to create an instance of an inner class , you need to have outer class instance.
so, InnerClass ic = new PrivateInnerClassTest().new InnerClass();

Yea Mua wrote:
At place //2, the compiler says that I reduced the visibility of method fun when overriding. But as far as I can see, they both have default accessibility, not less restricted. So what is the problem then?


by default, interface method has public visibility. so implementation class must give less restricted visibility(so in this case must be public) to implemented method.
 
Yea Mua
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seetharaman,

Thanks for the kind help!

Based on your point and my personal understanding, I think these two questions can be addressed as below:

Place //1: For a "regular" inner class as shown in this example (not static inner class, not local inner class or anonymous inner class), it can be instantiated by using new operator within the outer class's non static code; It can also be instantiated by using outerObj.new within the outer class's static code or outside outer class.

Place //2: Default access modifier for method in interface is public. In constrast, default access modifier for overriden method in class is default. So overriding a method without explicitly declaring public is actually reducing visibility.

 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic