Skip to content

Instantly share code, notes, and snippets.

@hgfernan
Created July 16, 2021 15:40
Show Gist options
  • Save hgfernan/d0325f1deede2eb64e4d8e7e51e758a8 to your computer and use it in GitHub Desktop.
Save hgfernan/d0325f1deede2eb64e4d8e7e51e758a8 to your computer and use it in GitHub Desktop.
Valid Java code, showing no access limits to encompassed classes
/**
*
*/
package tPrivateProtected;
/**
* @author hilton
*
*/
public class EncompassingClass {
static class PrivateClass {
private int privateMember = 0;
private PrivateClass() {
privateMember = 1;
}
private int sum(ProtectedClass pdc) {
return privateMember + pdc.protectedMember;
}
}
static class ProtectedClass {
protected int protectedMember = 0;
protected ProtectedClass() {
protectedMember = 1;
}
private int sum(PrivateClass pec) {
return protectedMember + pec.privateMember;
}
}
/**
* @param args
*/
public static void main(String[] args) {
PrivateClass pec = new PrivateClass();
ProtectedClass pdc = new ProtectedClass();
System.out.println("Private member " + pec.privateMember);
System.out.println("Protected member " + pdc.protectedMember + "\n");
System.out.println("Private sum: " + pec.sum(pdc));
System.out.println("Protected sum: " + pdc.sum(pec));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment