I want to rename your classes to Friend
, Shy
and Stranger
. The Friend
should be able to create Shy
, but Stranger
should not be able to.
This code will compile:
package com.sandbox;
public class Friend {
public void createShy() {
Shy shy = new Shy();
}
private static class Shy {
}
}
But this code won't:
package com.sandbox;
public class Stranger {
public void createShy() {
Friend.Shy shy = new Friend.Shy();
}
}
In addition, if we create a new class called FriendsChild
, this won't compile either:
package com.sandbox;
public class FriendsChild extends Friend {
public void childCreateShy() {
Shy shy = new Shy();
}
}
And this naming convention makes sense when you think about it. Just because I'm a friend of someone doesn't mean my child knows them.
Notice that all these classes are in the same package. As far as I can understand, this is the scenario you're looking for.