Package protected class can be accessed from another package.
Here are the sample code:
::::::::::::::
Bar.java
::::::::::::::
package bar;
import foo.Secrete;
public class Bar {
public static void main(String args[]) {
try {
Secrete s = new Secrete(" Hacker breakin");
System.out.println("The secrete message is \"" + s.getMsg() + "\"");
} catch (Exception e) {
e.printStackTrace();
}
}
}
::::::::::::::
Foo.java
::::::::::::::
package foo;
public class Foo {
public static void main(String args[]) {
Secrete s = new Secrete("Package only");
System.out.println("The secrete message is \"" + s.getMsg() + "\"");
}
}
class Secrete {
private String message;
Secrete(String msg) {
message = msg;
}
public String getMsg() {
return "Package secrete" + message;
}
private void setMsg(String msg) {
message = msg;
}
}
::::::::::::::
Secrete.java
::::::::::::::
package foo;
public class Secrete {
private String message;
public Secrete(String msg) {
message = msg;
}
public String getMsg() {
return "Fake class message" + message;
}
public void setMsg(String msg) {
message = msg;
}
}
Foo.java contained a package protected class Secrete in package foo.
Bar.java imports foo.Secrete class
Secrete.java is a public version of the Secrete class also packaged in foo
Compile Foo.java first:
% javac -d . Foo.java
% javac -d . Bar.java
Bar.java:4: Can't access class foo.Secrete. Only public classes and interfaces in other packages can be accessed.
import foo.Secrete;
^
1 error
Now compile the fake Secrete class and make it work:
% javac -d . Secrete.java
% javac -d . Bar.java
% java bar.Bar
The secrete message is "Fake class message Hacker breakin"
Now compile back the protected Secrete class and break into the Secrete class
with the precompiled Bar.class:
% javac -d . Foo.java
% java bar.Bar
The secrete message is "Package secrete Hacker breakin"
Here are the sample code:
::::::::::::::
Bar.java
::::::::::::::
package bar;
import foo.Secrete;
public class Bar {
public static void main(String args[]) {
try {
Secrete s = new Secrete(" Hacker breakin");
System.out.println("The secrete message is \"" + s.getMsg() + "\"");
} catch (Exception e) {
e.printStackTrace();
}
}
}
::::::::::::::
Foo.java
::::::::::::::
package foo;
public class Foo {
public static void main(String args[]) {
Secrete s = new Secrete("Package only");
System.out.println("The secrete message is \"" + s.getMsg() + "\"");
}
}
class Secrete {
private String message;
Secrete(String msg) {
message = msg;
}
public String getMsg() {
return "Package secrete" + message;
}
private void setMsg(String msg) {
message = msg;
}
}
::::::::::::::
Secrete.java
::::::::::::::
package foo;
public class Secrete {
private String message;
public Secrete(String msg) {
message = msg;
}
public String getMsg() {
return "Fake class message" + message;
}
public void setMsg(String msg) {
message = msg;
}
}
Foo.java contained a package protected class Secrete in package foo.
Bar.java imports foo.Secrete class
Secrete.java is a public version of the Secrete class also packaged in foo
Compile Foo.java first:
% javac -d . Foo.java
% javac -d . Bar.java
Bar.java:4: Can't access class foo.Secrete. Only public classes and interfaces in other packages can be accessed.
import foo.Secrete;
^
1 error
Now compile the fake Secrete class and make it work:
% javac -d . Secrete.java
% javac -d . Bar.java
% java bar.Bar
The secrete message is "Fake class message Hacker breakin"
Now compile back the protected Secrete class and break into the Secrete class
with the precompiled Bar.class:
% javac -d . Foo.java
% java bar.Bar
The secrete message is "Package secrete Hacker breakin"