Learn java access modifiers

Hey guys!! Welcome to flower brackets blog. Today we will learn java access modifiers.

In my previous posts you have seen the use of public, private and protected in defining class or method. These are called as access modifiers.

Also read – system array copy

Access modifiers control the visibility or scope of member variables, constructor and methods within the package or outside the package.

There are four types of access modifier private, protected, public and default.

private access modifier

If member variables, constructors and methods is defined with keyword “private”, then it can’t be accessed / visible within the class.

Also read – class and objects in java

Private access modifiers are restrictive one. Because it restricts the access of variables and methods outside package.

Classes and interfaces should not be defined with “private” keyword. Because classes and interfaces needs access outside the package or within.

class Demo
{
   // declaring method as private
   private void displayName()
   {
      System.out.println("Flower Brackets");
   }
}

public class PrivateDemo 
{
   public static void main(String[] args) 
   {
      Demo obj = new Demo();
      obj.displayName();
   }
}

Output:

Learn Java Access Modifiers


public access modifier

In this case class, method, variable and constructor declared with keyword “public” is accessible to entire java program.

If we want to access a class which is “public” outside the package then we can “import” that class to particular package.

package a1;

public class Example1
{
   public void printName()
   {
      System.out.println("Flower Brackets");
   }
}

package a2;
import a1.*;

public class PublicDemo 
{
   public static void main(String[] args) 
   {
      Example1 obj = new Example1();
      obj.printName();
   }
}

Output:

Flower Brackets


protected access modifier

Member variables and methods declared with keyword “protected” can be accessed within the same package or by subclasses of other package.

Like private access modifier, classes and interfaces can’t be declared with keyword “protected”.

package abc;
class Demo
{
   protected void printName()
   {
      System.out.println("Hello World");
   }
}

package def;
import abc.*;

class ProtectedExample extends Demo
{
   public static void main (String args[])
   {
      ProtectedExample obj = new ProtectedExample();
      obj.printName();
   }
}

Output:

Hello World


default access modifier

Method, member variable or class declared with no access modifier have “default” access modifier.

Variables, class or method having default access modifier are accessible within same package.

package abc;
class Demo
{
   void printName()
   {
      System.out.println("Flower Brackets");
   }
}

package def;
import abc.*;

class DefaultExample
{
   public static void main (String args[])
   {
      Demo obj = new Demo();
      obj.printName();
   }
}

Output:

Compile error


Here’s the relationship of above discussed access modifiers,

Learn Java Access Modifiers

Related Posts