Java access rights (class, interface, internal class, internal interface)
ref:
- Rookie tutorial - java Tutorial
- Java programming ideas [by Bruce Eckel]
Access modifier
Some types can only use partial access modifiers and have default modifiers
public, all classes are visible
private, the current class is visible
default, visible in the same package
protected, visible in the same package and descendants of different packages
Access rights of classes (excluding internal classes)
Class has only two access forms: public and default
- default class
- Can only be accessed by other classes in the same package.
- public class
- It can also be accessed by classes in other packages.
- Each compilation unit (file) can only have one public class
The public class name must have the same name as the file it is in
pubic classes may not exist in a compilation unit (file), but they are not commonly used
Class cannot be private or protected
- private constructor:
- If you don't want anyone (including in the same package) to have access to this class, you can specify all constructors as private. In this way, only its own static method can create the object of this class, and other classes can't use constructors.
- However, other packages with access to this class can call the public/default static member of this class, because the static member does not need an instance.
Access rights of interface (excluding internal interface)
- The interface has only two access forms: public and default
- Interfaces are implicitly abstract, and the abstract keyword is not necessary when declared
- Methods in the interface will be implicitly specified as public abstract (and can only be)
- The variables in the interface will be implicitly specified as public static final (and can only be) (that is, class constants)
- The methods in the interface cannot be implemented in the interface. The methods in the interface can only be implemented by the class that implements the interface
- After JDK 1.8, there can be static methods and method bodies in the interface.
Access rights of internal classes and internal interfaces (external access)
Non static internal classes and internal interfaces
Modifier:
public, all classes are visible
private, the current class is visible
default, visible in the same package
The internal class is understood as a member variable of the external class. Class is a class type, and the class defined by the class keyword is equivalent to creating a class instance (variable).
To use this method, you need to create an instance of an external class / interface: outer Inner inner = new Outer. new Inner();
Static internal classes and static internal interfaces
Just like using static methods, there is no need to create an instance of an external class / interface: outer Inner inner = new Outer. Inner();
The internal class in the interface is implicit public static and cannot be private
The internal interface in the interface is also implicit public static and cannot be private
In the example code, there are internal classes and internal interfaces in the interface, and the internal classes and internal interfaces in the class are the same.
Code example
File directory structure: InnerDemo and InnerDemo2 belong to different packages
|-InnerDemo
| |-OuterClass.java
| |-OuterClass2.java
| |-OuterInterface.java
| |-OuterInterface.java
| |-Test.java
|
|-InnerDemo2
| |-Test.java
OuterClass.java: nested inner classes with outer classes
public class OuterClass { public class InnerClass{} class InnerClass2{} private class InnerClass3{} }
OuterClass2.java: nested internal interfaces of external classes
public class OuterClass2 { public interface InnerInterface{} interface InnerInterface2{} private interface InnerInterface3{} //Innerface class implementation public class InnerInterfaceImp implements InnerInterface{} class InnerInterfaceImp2 implements InnerInterface{} private class InnerInterfaceImp3 implements InnerInterface{} //InnerInterface2 implementation class public class InnerInterface2Imp implements InnerInterface2{} class InnerInterface2Imp2 implements InnerInterface2{} private class InnerInterface2Imp3 implements InnerInterface2{} //InnerInterface3 implementation class public class InnerInterface3Imp implements InnerInterface3{} class InnerInterface3Imp2 implements InnerInterface3{} private class InnerInterface3Imp3 implements InnerInterface3{} }
OuterInterface.java: nested internal classes for external interfaces
/*The internal class in the interface is implicit public static and cannot be private*/ public interface OuterInterface { public static class InnerClass{} // private class InnerClass3{} / / illegal }
OuterInterface2.java: external interface nested internal interface
/*The internal class in the interface is implicit public static and cannot be private The internal interface in the interface is also implicit public static and cannot be private */ public interface OuterInterface2 { public static interface InnerInterface{} // private interface IninerInterface3{} / / illegal public static class InnerInterfaceImp implements InnerInterface{} }
InnerDemo/Test.java: test class
public class Test { public static void main(String[] args) { //Nested inner class of outer class OuterClass.InnerClass inner = new OuterClass().new InnerClass(); OuterClass.InnerClass2 inner2 = new OuterClass().new InnerClass2(); OuterClass.InnerClass3 inner3 = new OuterClass().new nnerClass3();//illegal //Nested internal interfaces of external classes OuterClass2.InnerInterface innerInterface = new OuterClass2().new InnerInterfaceImp(); OuterClass2.InnerInterface innerInterface_2 = new OuterClass2().new InnerInterfaceImp2(); OuterClass2.InnerInterface innerInterface_3 = new OuterClass2().new InnerInterfaceImp3();//illegal OuterClass2.InnerInterface2 innerInterface2 = new OuterClass2().new InnerInterface2Imp(); OuterClass2.InnerInterface2 innerInterface2_2 = new OuterClass2().new InnerInterface2Imp2(); OuterClass2.InnerInterface2 innerInterface2_3 = new OuterClass2().new InnerInterface2Imp3();//illegal OuterClass2.InnerInterface3 innerInterface3 = new OuterClass2().new InnerInterface3Imp();//illegal OuterClass2.InnerInterface3 innerInterface3_2 = new OuterClass2().new InnerInterface3Imp2();//illegal OuterClass2.InnerInterface3 innerInterface3_3 = new OuterClass2().new InnerInterface3Imp3();//illegal //Internal classes nested by external interfaces OuterInterface.InnerClass innerClass = new OuterInterface.InnerClass(); //External interface nested internal interface OuterInterface2.InnerInterface innerInterface1 = new uterInterface2.InnerInterfaceImp(); } }
InnerDemo2/Test.java: the test class is the same as above, but the syntax recognition results are different due to different packages
public class Test { public static void main(String[] args) { //Nested inner class of outer class OuterClass.InnerClass inner = new OuterClass().new InnerClass(); OuterClass.InnerClass2 inner2 = new OuterClass().new InnerClass2(); OuterClass.InnerClass3 inner3 = new OuterClass().new InnerClass3();//illegal //Nested internal interfaces of external classes OuterClass2.InnerInterface innerInterface = new OuterClass2().new InnerInterfaceImp(); OuterClass2.InnerInterface innerInterface_2 = new OuterClass2().new InnerInterfaceImp2(); OuterClass2.InnerInterface innerInterface_3 = new OuterClass2().new InnerInterfaceImp3();//illegal OuterClass2.InnerInterface2 innerInterface2 = new OuterClass2().new InnerInterface2Imp(); OuterClass2.InnerInterface2 innerInterface2_2 = new OuterClass2().new InnerInterface2Imp2(); OuterClass2.InnerInterface2 innerInterface2_3 = new OuterClass2().new InnerInterface2Imp3();//illegal OuterClass2.InnerInterface3 innerInterface3 = new OuterClass2().new InnerInterface3Imp();//illegal OuterClass2.InnerInterface3 innerInterface3_2 = new OuterClass2().new InnerInterface3Imp2();//illegal OuterClass2.InnerInterface3 innerInterface3_3 = new OuterClass2().new InnerInterface3Imp3();//illegal //Internal classes nested by external interfaces OuterInterface.InnerClass innerClass = new OuterInterface.InnerClass(); //External interface nested internal interface OuterInterface2.InnerInterface innerInterface1 = new OuterInterface2.InnerInterfaceImp(); } }
Internal [class / interface] accesses its external [class / interface]
Access to external classes / interfaces: create instances, use methods, and use variables
Internal class access external class
Analogical static method
Static internal class: external class instances can be created (other classes can also be created), but variables, methods and other internal classes of external classes cannot be used directly, because it is static and has no reference to external class instances, just as static methods cannot use non static variables.
Non static inner classes: all can be accessed
Internal class access
The internal class in the interface is implicit public static and cannot be private
Reference static inner class to access outer class
Internal interfaces access external classes
The external class needs to be accessed through the implementation class of the internal interface (which can be an internal class)
Refer to the internal class to access the external class
Internal interface access external interface
The external interface needs to be accessed through the implementation class of the internal interface
The internal interface in the interface is implicit public static and cannot be private
Reference static inner class to access outer class