Course Content
Core Java
About Lesson

In Java, access modifiers are keywords used to set the visibility and accessibility of classes, methods, constructors, and fields. They help to enforce encapsulation and control how the components of a program interact with each other.

Types of Access Modifiers

  1. Private: Access level is only within the class.
  2. Default: This is equivalent to not mentioning any access modifier. The access level is only within the package (package private). It cannot be accessed from outside the package.
  3. Protected: Access level is within the same package and outside the package in case of subclass.
  4. Public: Access level is everywhere.

Access Modifiers applied to

  1. Classes
  2. Methods
  3. Fields (Variables)
  4. Constructor

Access Modifiers for Classes

Already covered in topic : Types of Classes

Access Modifiers for Methods

  1. Public Methods: Method declared as public can be accessed from any other class
// File: com/example/PublicMethodExample.java
package com.example;

public class PublicMethodExample {
    public void publicMethod() {
        System.out.println("Public method");
    }
}

// File: com/test/TestPublicMethod.java
package com.test;

import com.example.PublicMethodExample;

public class TestPublicMethod {
    public static void main(String[] args) {
        PublicMethodExample example = new PublicMethodExample();
        example.publicMethod(); // Accessible from any other class
    }
}
  1. Protected Methods: A method declared as protected can be accessed within the same package and by subclasses in other packages.
// File: com/example/ProtectedMethodExample.java
package com.example;

public class ProtectedMethodExample {
    protected void protectedMethod() {
        System.out.println("Protected method");
    }
}

// File: com/example/TestProtectedMethod.java
package com.example;

public class TestProtectedMethod {
    public static void main(String[] args) {
        ProtectedMethodExample example = new ProtectedMethodExample();
        example.protectedMethod(); // Accessible within the same package
    }
}

// File: com/test/SubClass.java
package com.test;

import com.example.ProtectedMethodExample;

public class SubClass extends ProtectedMethodExample {
    public void accessProtectedMethod() {
        protectedMethod(); // Accessible from subclass in a different package
    }

    public static void main(String[] args) {
        SubClass sub = new SubClass();
        sub.accessProtectedMethod();
    }
}
  1. Default (Package-Private) Methods: A method declared with no access modifier (default) is accessible only within the same package. This is known as package-private access.
// File: com/example/DefaultMethodExample.java
package com.example;

public class DefaultMethodExample {
    void defaultMethod() {
        System.out.println("Default (package-private) method");
    }
}

// File: com/example/TestDefaultMethod.java
package com.example;

public class TestDefaultMethod {
    public static void main(String[] args) {
        DefaultMethodExample example = new DefaultMethodExample();
        example.defaultMethod(); // Accessible within the same package
    }
}

// File: com/test/TestDefaultMethodAccess.java
package com.test;

import com.example.DefaultMethodExample;

public class TestDefaultMethodAccess {
    public static void main(String[] args) {
        // DefaultMethodExample example = new DefaultMethodExample();
        // example.defaultMethod(); // Not accessible from a different package, uncommenting this line will cause a compile-time error
    }
}
  1. Private Methods: A method declared as private can be accessed only within the class in which it is declared.
// File: com/example/PrivateMethodExample.java
package com.example;

public class PrivateMethodExample {
    private void privateMethod() {
        System.out.println("Private method");
    }

    public void accessPrivateMethod() {
        privateMethod(); // Accessible within the same class
    }
}

// File: com/example/TestPrivateMethod.java
package com.example;

public class TestPrivateMethod {
    public static void main(String[] args) {
        PrivateMethodExample example = new PrivateMethodExample();
        // example.privateMethod(); // Not accessible outside the class, uncommenting this line will cause a compile-time error
        example.accessPrivateMethod(); // Accessing private method via public method
    }
}

Access Modifiers for Fields

  1. Public Fields: A field declared as public can be accessed from any other class
// File: com/example/PublicFieldExample.java
package com.example;

public class PublicFieldExample {
    public int publicField = 10;
}

// File: com/test/TestPublicField.java
package com.test;

import com.example.PublicFieldExample;

public class TestPublicField {
    public static void main(String[] args) {
        PublicFieldExample example = new PublicFieldExample();
        System.out.println(example.publicField); // Accessible from any other class
    }
}
  1. Protected Fields: A field declared as protected can be accessed within the same package and by subclasses in other packages.
// File: com/example/ProtectedFieldExample.java
package com.example;

public class ProtectedFieldExample {
    protected int protectedField = 20;
}

// File: com/example/TestProtectedField.java
package com.example;

public class TestProtectedField {
    public static void main(String[] args) {
        ProtectedFieldExample example = new ProtectedFieldExample();
        System.out.println(example.protectedField); // Accessible within the same package
    }
}

// File: com/test/SubClass.java
package com.test;

import com.example.ProtectedFieldExample;

public class SubClass extends ProtectedFieldExample {
    public void accessProtectedField() {
        System.out.println(protectedField); // Accessible from subclass in a different package
    }

    public static void main(String[] args) {
        SubClass sub = new SubClass();
        sub.accessProtectedField();
    }
}
  1. Default (Package-Private) Fields: A field declared with no access modifier (default) is accessible only within the same package
// File: com/example/DefaultFieldExample.java
package com.example;

public class DefaultFieldExample {
    int defaultField = 30;
}

// File: com/example/TestDefaultField.java
package com.example;

public class TestDefaultField {
    public static void main(String[] args) {
        DefaultFieldExample example = new DefaultFieldExample();
        System.out.println(example.defaultField); // Accessible within the same package
    }
}

// File: com/test/TestDefaultFieldAccess.java
package com.test;

import com.example.DefaultFieldExample;

public class TestDefaultFieldAccess {
    public static void main(String[] args) {
        // DefaultFieldExample example = new DefaultFieldExample();
        // System.out.println(example.defaultField); // Not accessible from a different package, uncommenting this line will cause a compile-time error
    }
}
  1. Private Fields: A field declared as private can be accessed only within the class in which it is declared.
// File: com/example/PrivateFieldExample.java
package com.example;

public class PrivateFieldExample {
    private int privateField = 40;

    public int getPrivateField() {
        return privateField;
    }
}

// File: com/example/TestPrivateField.java
package com.example;

public class TestPrivateField {
    public static void main(String[] args) {
        PrivateFieldExample example = new PrivateFieldExample();
        // System.out.println(example.privateField); // Not accessible outside the class, uncommenting this line will cause a compile-time error
        System.out.println(example.getPrivateField()); // Accessing private field via public method
    }
}

Access Modifiers for Constructors

  1. Public Constructors: A constructor declared as public can be accessed from any other class. This means instances of the class can be created from anywhere.
// File: com/example/PublicConstructorExample.java
package com.example;

public class PublicConstructorExample {
    public PublicConstructorExample() {
        System.out.println("Public constructor");
    }
}

// File: com/test/TestPublicConstructor.java
package com.test;

import com.example.PublicConstructorExample;

public class TestPublicConstructor {
    public static void main(String[] args) {
        PublicConstructorExample example = new PublicConstructorExample(); // Accessible from any other class
    }
}
  1. Protected Constructors: A constructor declared as protected can be accessed within the same package and by subclasses in other packages. This allows subclasses to create instances of the class, even if they are in different packages.
// File: com/example/ProtectedConstructorExample.java
package com.example;

public class ProtectedConstructorExample {
    protected ProtectedConstructorExample() {
        System.out.println("Protected constructor");
    }
}

// File: com/example/TestProtectedConstructor.java
package com.example;

public class TestProtectedConstructor {
    public static void main(String[] args) {
        ProtectedConstructorExample example = new ProtectedConstructorExample(); // Accessible within the same package
    }
}

// File: com/test/SubClass.java
package com.test;

import com.example.ProtectedConstructorExample;

public class SubClass extends ProtectedConstructorExample {
    public SubClass() {
        super(); // Accessible from subclass in a different package
    }

    public static void main(String[] args) {
        SubClass sub = new SubClass();
    }
}

  1. Default (Package-Private) Constructors: A constructor declared with no access modifier (default) is accessible only within the same package. This means instances of the class can only be created by other classes in the same package.
// File: com/example/DefaultConstructorExample.java
package com.example;

public class DefaultConstructorExample {
    DefaultConstructorExample() {
        System.out.println("Default (package-private) constructor");
    }
}

// File: com/example/TestDefaultConstructor.java
package com.example;

public class TestDefaultConstructor {
    public static void main(String[] args) {
        DefaultConstructorExample example = new DefaultConstructorExample(); // Accessible within the same package
    }
}

// File: com/test/TestDefaultConstructorAccess.java
package com.test;

import com.example.DefaultConstructorExample;

public class TestDefaultConstructorAccess {
    public static void main(String[] args) {
        // DefaultConstructorExample example = new DefaultConstructorExample(); // Not accessible from a different package, uncommenting this line will cause a compile-time error
    }
}
  1. Private Constructors: A constructor declared as private can be accessed only within the class in which it is declared. This restricts the creation of instances of the class to within the class itself. Private constructors are often used in singleton patterns and utility classes where instance creation needs to be controlled.
// File: com/example/PrivateConstructorExample.java
package com.example;

public class PrivateConstructorExample {
    private PrivateConstructorExample() {
        System.out.println("Private constructor");
    }

    // Static method to access private constructor
    public static PrivateConstructorExample createInstance() {
        return new PrivateConstructorExample();
    }
}

// File: com/example/TestPrivateConstructor.java
package com.example;

public class TestPrivateConstructor {
    public static void main(String[] args) {
        // PrivateConstructorExample example = new PrivateConstructorExample(); // Not accessible outside the class, uncommenting this line will cause a compile-time error
        PrivateConstructorExample example = PrivateConstructorExample.createInstance(); // Accessing private constructor via public method
    }
}

Scroll to Top