• 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

static class protected access

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package cert1;

public class Outer {
protected static class Inner {
}
}


package cert2;
import static cert1.Outer.Inner; //error

class Outer1 extends Inner { //error


}


Why is so?
If I make the Inner class public, it works fine. Why doesn't it work in case of protected access.

Please guide if I am missing anything!

Thanks,
cmbhatt
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can only use protected classes within the same package, or through inheritance (i.e. another class subclassing the protected class). Read up a little bit on java class modifiers to get a good briefing on this subject, the 3 access modifiers are public, protected, and private. But there are four access levels, public, protected, default, and private. Default is only used when you do not specify another modifier, and it means package level access (i.e. only classes in the same package can access that class).
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dustin,

First of all no top level classes can be made protected. And secondly my question is regarding, nested static class that is made protected, assuming it could be accessed through inheritance. My question is, can be extend that nested class in another package, I know I need not to inherit the Outer class that is containing the static inner class. Although it works if I make it public.

Thanks,
 
Dustin Johnson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize, perhaps my answer was a little more broad than I intended. Yes, I agree that top level classes can only be Public, or Default. However as you mentioned, nested/inner classes are allowed to be any of the four levels.

You are attempting to extend class Inner, which is in the package cert1. You have defined class Inner as a protected member of class Outer, which is a public class. This means that you can only access (see) the members of class Outer which are public unless you are (1) In the same package � cert1, or (2)You are a subclass of class Outer, in which you receive a copy of the protected variable yourself. As a subclass of class Outer placed in a separate package, you would not be able to access your parent class� version of class Inner, you would only have access to your own. However, a subclass in the same package would not have this hindrance.

Hence, your class Outer1, being in a different package, cert2, cannot see the class which you are attempting to extend (Inner). This is because it is in a different package and is a protected member.

You have two solutions to be able to extend class Inner with Outer1. You can place Outer1 in the same package: cert1, in which you would be able to access or extend any members or classes which were public, protected, or default. Or, you can promote class Inner to public, which would enable any class from any package to extend class Inner.
 
reply
    Bookmark Topic Watch Topic
  • New Topic