⬅ Previous Topic
Java Class ObjectsNext Topic ⮕
Java Static Variables in Classes⬅ Previous Topic
Java Class ObjectsNext Topic ⮕
Java Static Variables in ClassesIn Java, access modifiers are keywords that set the visibility or accessibility of classes, methods, constructors, and variables. They help define who can see or use a particular piece of code. Think of them like the security levels of a building—some areas are open to all, while others are restricted.
Access Modifiers promote encapsulation. By carefully selecting access levels, you keep your code modular, maintainable, and secure from unintended interference.
When a class or method is marked public
, it’s like declaring it as “open for business.” Any class from any package can access it.
public class MyClass {
public void greet() {
System.out.println("Hello from a public method!");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.greet();
}
}
Hello from a public method!
Private members are strictly confined to the class they belong to. No external class can access them—not even a subclass.
public class SecretVault {
private String secretCode = "1234";
private void whisper() {
System.out.println("Accessing private secrets...");
}
public void reveal() {
whisper();
System.out.println("Code is: " + secretCode);
}
}
public class AccessAttempt {
public static void main(String[] args) {
SecretVault vault = new SecretVault();
vault.reveal();
// vault.secretCode; // Error
// vault.whisper(); // Error
}
}
Accessing private secrets...
Code is: 1234
Trying to access secretCode
or whisper()
directly will throw a compile-time error. They’re truly off-limits.
Protected members can be accessed from:
// In package animals
package animals;
public class Animal {
protected void makeSound() {
System.out.println("Animal makes a sound");
}
}
// In package zoo
package zoo;
import animals.Animal;
public class Dog extends Animal {
public void bark() {
makeSound(); // Allowed due to protected
System.out.println("Dog barks!");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.bark();
}
}
Animal makes a sound
Dog barks!
Here, the subclass Dog
accesses makeSound()
from a different package because it's protected
.
When no modifier is specified, Java treats it as default access. Such members are only visible within the same package.
// In file PackageClass.java
package mypackage;
class PackageClass {
void display() {
System.out.println("Default access: only in mypackage");
}
}
// In another file in same package
package mypackage;
public class Demo {
public static void main(String[] args) {
PackageClass pc = new PackageClass();
pc.display(); // Works fine
}
}
Default access: only in mypackage
If another class in a different package tries to use PackageClass
or call display()
, it will result in an error.
Modifier | Same Class | Same Package | Subclass (Diff Package) | Other Packages |
---|---|---|---|---|
public | Yes | Yes | Yes | Yes |
protected | Yes | Yes | Yes | No |
default | Yes | Yes | No | No |
private | Yes | No | No | No |
Here’s a quick decision guide:
⬅ Previous Topic
Java Class ObjectsNext Topic ⮕
Java Static Variables in ClassesYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.