public: anywhere.
protected: accessible by classes within in the same package, and sub-class outside the package.
default: only access within the class and classes in the same package.
private: only access within the class, not accessible even by the sub-classes.
The following example will show a compiling error, for A constructor using default modifier cannot be accessible outside the package. So it causes sub-class to init. error as calling super().
//File A.java
package a;
public class A {
A() {
}
public void print() {
System.out.println("A");
}
}
//File B.java
package b;
import a.*;
public class B extends A {
B() {
}
public void print() {
System.out.println("B");
}
public static void main(String[] args) {
new B();
}
}
class normally being declared as public or default accessible.private class is invalid modifier for the class, actually it doesn't make any sense; a private class should be declared as an inner class.
No comments:
Post a Comment