Programming





Access Modifiers in Java

Access Modifiers in Java


by Administrator | 05-Apr-2020
Java

Tags:  Access Level  Access Modifier  Private  Protected  Default  Public

Share: 


Access modifiers are used to provide or restrict the scope of a class and it’s members like methods, constructors, variables, etc.

Following are access levels defined in Java:

  1. Private level
  2. Default/package level
  3. Protected level
  4. Public level

Following are the access modifiers:

  1. Private
  2. Protected
  3. Public
  1. Private

Private members are only accessible within the same class. It cannot be accessed outside the class not even in the sub-class. Private is the most restrictive scope/level. 

Program

class Shoe {
    private int size = 10;
      private void printSize() {
        System.out.println("The size is: " + size);
      }
}
public class PrivateTest {
    public static void main(String args[]) {
      Shoe shoe = new Shoe();
      System.out.println(shoe.size); // Compile Time Error: The field Shoe.size is not visible
      shoe.printSize(); // Compile Time Error: The method printSize() from the type Shoe is not visible
    }
}

2. Default/package

This type of member will be accessed within the same package in sub-class or non-sub-class. A default member cannot be accessed by any other class outside the package. This is the default modifier i.e. if you are not using any modifier then it is default, by default.

Program

package com.prgrmmng;
public class Shoe {
    int size = 10; 
      void printSize() {
        System.out.println("The size is: " + size);
    }
}

 

package com.google;
import com.prgrmmng.Shoe; 
public class DefaultTest {
    public static void main(String args[]) {
      Shoe shoe = new Shoe();
      shoe.printSize(); // Compile Time Error: The method printSize() from the type Shoe is not visible
      System.out.println(shoe.size); // Compile Time Error: The field Shoe.size is not visible
    }
}

3. Protected

Protected members are accessible by any class within the same package. It can also be accessible in a sub-class outside the package.

Program

package com.prgrmmng;
public class Shoe {
    protected int size = 10; 
    protected void printSize() {
      System.out.println("The size is: " + size);
    }
}

 

package com.google; 
import com.prgrmmng.Shoe; 
public class DefaultTest extends Shoe {
    public static void main(String args[]) {
      DefaultTest defaultTest = new DefaultTest();
      defaultTest.printSize();
      System.out.println(defaultTest.size);
    }
}

Output

The size is: 10
10

4. Public

Public modifiers are accessible throughout the application. There are no restrictions on a public class and its members. Public is the least restrictive scope/level.

Program

package com.prgrmmng; 
public class Shoe {
    public int size = 10; 
    public void printSize() {
      System.out.println("The size is: " + size);
    }
}

 

package com.google; 
import com.prgrmmng.Shoe; 
public class PublicTest {
    public static void main(String args[]) {
      Shoe defaultTest = new Shoe();
      defaultTest.printSize();
      System.out.println(defaultTest.size);
    }
}

Output

The size is: 10
10
 

Tabular Representation of Scopes of various Modifiers 

Modifier

Same class

Same package

Subclass (Same package)

Subclass (different package)

Outside Class

Private

Yes

No

No

No

No

Default

Yes

Yes

Yes

No

No

Protected

Yes

Yes

Yes

Yes

No

Public

Yes

Yes

Yes

Yes

Yes


Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.