Modifiers for controlling access to a class, method, or variable: public, protected, and private

Modifiers for controlling access to a class, method, or variable: public, protected, and private

Previous Home Next

 

the words public, protected and private at the beginning of class's method declarations. 
These keywords are called the access modifiers in the Java language syntax, and can be defined as...
 keywords that help set the visibility and accessibility of a class, its member variables, and methods

Public Access modifier

If a class has public visibility, the class can be referenced by anywhere in the program.
 
If a variable is defined in a public class and it has public visibility,
 the variable can be reference anywhere in the application through the class it is defined in.

If a method is defined in a public class and it has public visibility,
the method can be called anywhere in the application through the class it is defined in


Private Access modifiers

If a class has private visibility,
it can happen only if the class is defined inside in an other class, the class can be accessed only in the outer class.

If a variable has private visibility, the variable can be accessed only in the class it is defined in.

If a method has private visibility, the method can be called only in the class it is defined in.

Protected Access Modifiers

If a class has protected visibility it is visible inside its package only. 

If  a variable has protected visibility allows the class to access it and the subclass to access it.inside the same package.

If a method has protected visibility allows the class to access it and the subclass to access it inside the same package
.
 


 

Previous Home Next