Data Types and Operators
(Non-Primitive Data Types)
Control Flow Statements
Conditional Statements
Looping Statements
Branching Statements
Object-Oriented Programming (OOP)
Exception Handling
Collections Framework
Overview of Collections
Java I/O
Multithreading
GUI Programming with Swing
Advanced Topics
JAVA CODE
Java Basics
Working with Objects
Arrays, Conditionals, and Loops
Creating Classes and Applications in Java
More About Methods
Java Applet Basics
Graphics, Fonts, and Color
Simple Animation and Threads
More Animation, Images, and Sound
Managing Simple Events and Interactivity
Creating User Interfaces with the awt
Windows, Networking, and Other Tidbits
Modifiers, Access Control, and Class Design
Packages and Interfaces
Exceptions
Multithreading
Streams and I/O
Using Native Methods and Libraries
Under the Hood
Java Programming Tools
Working with Data Structures in Java
Advanced Animation and Media
Fun with Image Filters
Client/Server Networking in Java
Emerging Technologies
appendix A :- Language Summary
appendix B :- Class Hierarchy Diagrams
appendix C The Java Class Library
appendix D Bytecodes Reference
appendix E java.applet Package Reference
appendix F java.awt Package Reference
appendix G java.awt.image Package Reference
appendix H java.awt.peer Package Reference
appendix I java.io Package Reference
appendix J java.lang Package Reference
appendix K java.net Package Reference
appendix L java.util Package Reference

Access modifiers in Java are keywords that set the accessibility of classes, methods, and other members. They determine whether other classes can use a particular field or invoke a particular method. Java has four main access modifiers:

  1. Public
  2. Protected
  3. Default (Package-Private)
  4. Private

1. Public

The ‘public‘ modifier has the widest scope among all access modifiers. When a member is declared ‘public‘, it can be accessed from any other class, regardless of the package it resides in.

				
					public class MyClass {
    public int myPublicVar;

    public void myPublicMethod() {
        // method logic
    }
}

				
			

2. Protected

The ‘protected‘ modifier provides more restricted access compared to ‘public‘. A ‘protected‘ member is accessible within its own package and by subclasses, even if they are in different packages.

				
					package package1;

public class MyClass {
    protected int myProtectedVar;

    protected void myProtectedMethod() {
        // method logic
    }
}

package package2;

import package1.MyClass;

public class AnotherClass extends MyClass {
    public void anotherMethod() {
        myProtectedVar = 10; // Accessible because AnotherClass is a subclass
        myProtectedMethod(); // Accessible because AnotherClass is a subclass
    }
}

				
			

3. Default (Package-Private)

If no access modifier is specified, the default access level is used, also known as package-private. A member with default access is only accessible within its own package. It is not accessible from outside the package.

				
					package package1;

class MyClass {
    int myDefaultVar;

    void myDefaultMethod() {
        // method logic
    }
}

package package2;

import package1.MyClass;

public class AnotherClass {
    public void anotherMethod() {
        MyClass obj = new MyClass();
        // obj.myDefaultVar = 10; // Not accessible
        // obj.myDefaultMethod(); // Not accessible
    }
}

				
			

4. Private

The ‘private‘ modifier is the most restrictive access level. A ‘private‘ member is only accessible within the class in which it is declared. It is not accessible from any other class.

				
					public class MyClass {
    private int myPrivateVar;

    private void myPrivateMethod() {
        // method logic
    }

    public void anotherMethod() {
        myPrivateVar = 10; // Accessible
        myPrivateMethod(); // Accessible
    }
}

public class AnotherClass {
    public void anotherMethod() {
        MyClass obj = new MyClass();
        // obj.myPrivateVar = 10; // Not accessible
        // obj.myPrivateMethod(); // Not accessible
    }
}

				
			

Summary

  • Public: Accessible from everywhere.
  • Protected: Accessible within the same package and subclasses.
  • Default (Package-Private): Accessible only within the same package.
  • Private: Accessible only within the same class.

Use Cases and Best Practices

  • Public: Use for classes, methods, or variables that need to be accessible from any other class.
  • Protected: Useful in class hierarchies where subclasses need access to the parent class’s methods or variables.
  • Default: Use when you want to restrict access to the members within the same package.
  • Private: Use for encapsulation, hiding the internal state and requiring all interaction through an object’s methods.

Examples

Public

				
					public class Vehicle {
    public int speed;
    
    public void setSpeed(int speed) {
        this.speed = speed;
    }
}

				
			

Protected

				
					public class Car extends Vehicle {
    protected String model;

    protected void setModel(String model) {
        this.model = model;
    }
}

				
			

Default (Package-Private)

				
					class Truck {
    int loadCapacity;

    void setLoadCapacity(int loadCapacity) {
        this.loadCapacity = loadCapacity;
    }
}

				
			

Private

				
					public class Bike {
    private String type;

    private void setType(String type) {
        this.type = type;
    }

    public void displayType() {
        setType("Mountain");
        System.out.println("Bike type: " + type);
    }
}

				
			

Using these access modifiers properly helps in achieving encapsulation, modularity, and a well-defined interface for your classes and objects.

Scroll to Top