Java Access Modifiers
In Java, there are four access modifiers that can be used to control the visibility and access of class members such as fields, methods, and constructors. These access modifiers are:
- Public: Accessible from anywhere, even outside the package.
- Protected: Accessible within the same package and subclasses outside the package.
- Default (no modifier): Accessible within the same package.
- Private: Accessible only within the same class.
Syntax
The access modifiers are placed before the class member's declaration. The following is the syntax for each access modifier:
public class MyClass {
public int publicField;
protected int protectedField;
int defaultField;
private int privateField;
public void publicMethod() { }
protected void protectedMethod() { }
void defaultMethod() { }
private void privateMethod() { }
}
Example
public class Person {
public String name;
protected int age;
String gender;
private String ssn;
public Person(String name, int age, String gender, String ssn) {
this.name = name;
this.age = age;
this.gender = gender;
this.ssn = ssn;
}
public void printPerson() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Gender: " + gender);
}
}
In the above example, the name
field is public, the age
field is protected, the gender
field is default, and the ssn
field is private. The printPerson()
method is public and can be accessed from anywhere.
Explanation
- Public: When a class member is defined as public, it can be accessed from anywhere, even outside the package. For example, a public method defined in a class can be called from any other class.
- Protected: When a class member is defined as protected, it can be accessed within the same package and subclasses outside the package. For example, a protected field defined in a class can be accessed by any subclass of that class, even if it is in a different package.
- Default (no modifier): When a class member has no access modifier, it is said to have default access. It can be accessed within the same package only. For example, a default method defined in a class can only be called from another class in the same package.
- Private: When a class member is defined as private, it can only be accessed within the same class. For example, a private field defined in a class can only be accessed by methods within that same class.
Use
Access modifiers are used to control the visibility and access of class members. Public members are used to provide access to anyone who wants it, while private members are used to restrict access to only the class in which they are defined.
Protected and default access modifiers are used to control access in more specific ways. Protected members can be accessed by subclasses and other classes in the same package, while default members can be accessed only by other classes in the same package.
Important Points
- Access modifiers are used to control the visibility and access of class members.
- Public members are accessible from anywhere, private members are only accessible within the same class, and protected members are accessible within the same package and subclasses outside the package.
- Default access is used when no access modifier is defined.
- Access modifiers are used to control access to members in a more specific way.
Summary
Access modifiers are a fundamental concept in Java that allow you to control the visibility and access of your class members. Public members can be accessed from anywhere, private members are accessible only within the same class, and protected and default members provide more specific control over access.