Saturday, 21 September 2019

Java Tutorials | Access Modifiers in Java

The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can change the access level of fields, constructors, methods, and class by applying the access modifier on it.

There are four types of Java access modifiers:
1.    Private Access modifier
2.   Default Access modifier
3.    Protected Access modifier
4.   Public Access modifier

To understand Java Access Modifiers, see below given table:

1. Private Access modifier
It 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 the same package will not be able to access these members. Top level classes or interface cannot be declared private as “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

// Error while calling a private method from different class
public class ClassA {
     private void display() {
          System.out.println("GeeksforGeeks");
     }
}

class ClassB {
     public static void main(String args[]) {
          ClassA obj = new ClassA();
          // trying to access private method of another class
          obj.display();
     }
} 
Output:
error: display() has private access in ClassA obj.display();               

2. Default Access modifier
When no access modifier is specified for a class, method or data member – It is said to be having the default access modifier by default. Default modifiers are accessible only within the same package.

Example, we will create two packages and the classes in the packages will be having the default access modifiers and we will try to access a class from one package from a class of second package.

// ClassB is having Default access modifier
package com.b;
class ClassB {
     void display() {
          System.out.println("Hello World!");
     }
}

package com.a;
import com.b.ClassB;
public class ClassA {
     public static void main(String args[]) {
          ClassB obj = new ClassB();
          obj.display();
     }
}
Output: The method display() from the type ClassB is not visible

3. Protected Access modifier
The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.

// ClassB is having protected access modifier
package com.b;
class ClassB {
     protected void display() {
          System.out.println("Hello World!");
     }
}

package com.a;
import com.b.ClassB;
public class ClassA {
     public static void main(String args[]) {
          ClassA obj = new ClassA();
          obj.display();
     }
}
Output: Hello World!

4. Public Access modifier
The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.
There are many non-access modifiers, such as static, abstract, synchronized, native, volatile, transient, etc. Here, we are going to learn the access modifiers only.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...