0% found this document useful (0 votes)
2 views

access control

The document explains access control in Java, detailing the four main access modifiers: public, protected, default (package-private), and private. Each modifier defines the visibility and accessibility of classes and members, with public being accessible from anywhere, protected allowing access within the same package and subclasses, default restricting access to the same package, and private limiting access to the same class only. A summary table is provided to illustrate the access levels for each modifier in relation to different contexts.

Uploaded by

gyashika198
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

access control

The document explains access control in Java, detailing the four main access modifiers: public, protected, default (package-private), and private. Each modifier defines the visibility and accessibility of classes and members, with public being accessible from anywhere, protected allowing access within the same package and subclasses, default restricting access to the same package, and private limiting access to the same class only. A summary table is provided to illustrate the access levels for each modifier in relation to different contexts.

Uploaded by

gyashika198
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

In Java, access control refers to the mechanisms that restrict access to classes, methods, and

fields based on their visibility or scope. Java provides several access modifiers to help define
how and where a class or member can be accessed. There are four main access modifiers in
Java:

1. public

 Visibility: Any class or method marked as public can be accessed from any other
class, regardless of the package.
 Usage: Typically used for methods or classes that need to be accessible from
anywhere in the application.

public class Example {


public int number;

public void display() {


System.out.println("Hello!");
}
}

2. protected

 Visibility: Members marked as protected can be accessed within the same package
or by subclasses (even if they are in a different package).
 Usage: Useful for inheritance, when you want to allow access to certain members in
child classes, but not to the general public.

public class Example {


protected int number;

protected void display() {


System.out.println("Hello!");
}
}

3. default (no modifier)

 Visibility: If no access modifier is specified, the class or member is considered to


have default or package-private access. This means it is only accessible within the
same package.
 Usage: This is the default access level when no modifier is specified.

class Example {
int number; // default access

void display() { // default access


System.out.println("Hello!");
}
}
4. private

 Visibility: Members marked as private can only be accessed within the same class.
No external class, including subclasses, can access private members directly.
 Usage: Used to encapsulate data and hide it from other classes.

public class Example {

private int number;

private void display() {


System.out.println("Hello!");
}
}

Summary of Access Levels:

Modifier Same Class Same Package Subclass World


public Yes Yes Yes Yes
protected Yes Yes Yes No
default Yes Yes No No
private Yes No No No

You might also like