0% found this document useful (0 votes)
3 views10 pages

2 Access Modifiers in Java

The document explains access modifiers in Java, categorizing them into private, default, protected, and public, each with specific accessibility rules. It highlights the importance of choosing the correct modifier for encapsulation and inheritance, as well as the rules for method overriding. Key takeaways emphasize understanding access levels and maintaining or widening access in inheritance scenarios.

Uploaded by

Tareq Noorzai
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)
3 views10 pages

2 Access Modifiers in Java

The document explains access modifiers in Java, categorizing them into private, default, protected, and public, each with specific accessibility rules. It highlights the importance of choosing the correct modifier for encapsulation and inheritance, as well as the rules for method overriding. Key takeaways emphasize understanding access levels and maintaining or widening access in inheritance scenarios.

Uploaded by

Tareq Noorzai
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

Access Modifiers in Java

Two types: access modifiers and non-access modifiers

Specifies accessibility/scope of fields, methods, constructors, classes

by Web Developer Tariq


Types of Access Modifiers
1 Private
Only within class

2 Default
Only within package

3 Protected
Within package and child classes

4 Public
Accessible everywhere
Access Modifier Comparison
Within Class Within Package Outside Package Outside Package
(Subclass)

Private Y N N N

Default Y Y N N

Protected Y Y Y N

Public Y Y Y Y
Private Access Modifier
Scope Usage
Accessible only within the class For data members and
methods

Example
private int data = 40;
Private Constructor
Purpose
Prevents instance creation outside class

Usage
Singleton pattern, utility classes

Example
private A(){}
Default Access Modifier

Usage
2
When no modifier is specified

Scope 1
Accessible only within package

Restriction

3 Cannot access from outside package


Protected Access Modifier

Scope Usage Inheritance


Within package and outside Data members, methods, Accessible to subclasses in
through inheritance constructors different packages
Public Access Modifier
Widest Scope
1 Accessible everywhere

Usage
2
Classes, methods, data members

Example
3
public void msg(){...}
Method Overriding and Access Modifiers
Rule
1
Overridden method must not be more restrictive

Example
2
Protected in parent, can't be default in child

Reason
3
Ensures consistent access across inheritance
Key Takeaways
1 Understand Access Levels
Private, Default, Protected, Public

2 Choose Wisely
Use appropriate modifier for encapsulation

3 Consider Inheritance
Protected for subclass access

4 Remember Overriding Rules


Maintain or widen access

You might also like