public is the only access modifier that can be applied on a Java class.
Classes with public access can be accessed by other classes in all packages. Classes with default access level (no public access modifier applied) can be accessed by other classes within the same package only.
Classes with public access can be accessed by other classes in all packages. Classes with default access level (no public access modifier applied) can be accessed by other classes within the same package only.
package info.icontraining.p1;
import info.icontraining.p2.A;
public class Test {
public void m1() {
A a = new A();
// B b = new B(); // un-commenting causes a compile error
}
}
package info.icontraining.p2;
import info.icontraining.p1.Test;
public class A {
public void m1() {
Test t = new Test();
}
}
package info.icontraining.p2;
import info.icontraining.p1.Test;
class B {
public void m1() {
Test t = new Test();
}
}
Classes A and B (present in the same package ) can access class Test (present in a different package).
Class Test can access A (since it is public), but cannot access B (since it is default).
No comments:
Post a Comment