WEEK 11 - Access Modifiers in Java
WEEK 11 - Access Modifiers in Java
Modifiers in Java
WEEK 11
In Java, a modifier has a reserved keyword that is
included in the definition of class, method, and
variables. A modifier adds some meanings to
these definitions. Modifiers are also
called specifiers.
package defaultPackage;
class Logger {
void message(){
System.out.println(“This is a message”);
}
}
101
Protected Access Modifier
class ABC{
This example throws compilation
private double num = 100; error because we are trying to
private int square(int a){ access the private data member
return a*a; and method of class ABC in the
} class Example. The private data
} member and method are only
accessible within the class.
public class Example{
public static void main(String args[]){
ABC obj = new ABC();
System.out.println(obj.num);
System.out.println(obj.square(10));
}
}
OUTPUT:
Compile - time error
THANK
YOU!