0% found this document useful (0 votes)
1 views3 pages

Access Modifiers

Access Modifiers in Java determine the visibility of classes, methods, constructors, and variables. There are four types: public (accessible from any class), protected (accessible in the same package and subclasses), default (accessible only within the same package), and private (accessible only within the same class). Each modifier serves different purposes for encapsulation and access control.

Uploaded by

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

Access Modifiers

Access Modifiers in Java determine the visibility of classes, methods, constructors, and variables. There are four types: public (accessible from any class), protected (accessible in the same package and subclasses), default (accessible only within the same package), and private (accessible only within the same class). Each modifier serves different purposes for encapsulation and access control.

Uploaded by

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

Access Modifiers in Java

Access Modifiers in Java control the visibility (accessibility) of


classes, methods, constructors, and variables to other classes or
packages.

Types of Access Modifiers:


Modifier Access Level Accessible From
public Most accessible Any class in any
package
protected Accessible in the Same package or
same package and subclass (even if
in subclasses (even it's in a different
in other packages) package)
no modifier Also called default Only within the
or package-private same package
private Most restrictive Only within the
same class

Explanation with Examples:

1. public
Visible everywhere (any package/class can access it).

public class Animal {


public void speak() {
System.out.println("Animal speaks");
}
}

2. private
Only accessible within the same class.
Used for encapsulation (e.g., private fields with public
getters/setters).
class Animal {
private void breathe() {
System.out.println("Animal breathes");
}
}

3. protected
Accessible in the same package and in subclasses (even if
subclass is in another package).

class Animal {
protected void eat() {
System.out.println("Animal eats");
}
}

4. Default (No Modifier)


No keyword is written.
Accessible only within the same package.

class Animal {
void run() {
System.out.println("Animal runs");
}
}

Summary Table
Modifier Same Same Subclass Other
Class Package (other Package
package)
public ✅ ✅ ✅ ✅
protected ✅ ✅ ✅ ❌ (unless
subclass )
default ✅ ✅ ❌ ❌
private ✅ ❌ ❌ ❌

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy