The private access modifier is specified using the keyword private. The methods or data members declared as private are accessible only within the class in which they are declared. Any other class of same package will not be able to access these members. Top level Classes or interface can not be declared as private because private means “only visible within the enclosing class”. protected means “only visible within the enclosing class and any subclasses”. Hence these modifiers in terms of application to classes, they apply only to nested classes and not on top level classes In the below example, we will create two classes A and B within same package p1. We will declare a method in class A as private and try to access this method from class B and see the result.
The above code display error message: The method display() from the type A is not visible, if you are in Eclipse.