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

Access Modifiers in Java

The document explains access modifiers in Java, which restrict the scope of classes, methods, and data members. There are four types of access modifiers: Default, Private, Protected, and Public, each with specific accessibility rules. Additionally, it discusses encapsulation, emphasizing its importance in controlling data access and facilitating easier class creation.

Uploaded by

Sanjay S
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 views8 pages

Access Modifiers in Java

The document explains access modifiers in Java, which restrict the scope of classes, methods, and data members. There are four types of access modifiers: Default, Private, Protected, and Public, each with specific accessibility rules. Additionally, it discusses encapsulation, emphasizing its importance in controlling data access and facilitating easier class creation.

Uploaded by

Sanjay S
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

JAVA PROGRAMMING

TCE Online Course – Java programming 1


Access modifiers
Access modifiers in Java helps to restrict the scope of a class, constructor, variable, method, or data member.
There are four types of access modifiers in java
1. Default – without any keyword
2. Private
3. Protected
4. Public

TCE Online Course – Java Programming 2


Default keyword

Default: When no access modifier is specified for a class, method, or data member – It is said to be having
the default access modifier by default.
The data members, class or methods which are not declared using any access modifiers i.e. having default access
modifier are accessible only within the same package.

[Link] Access control Available


1 Same class YES
2 Same package subclass YES
3 Same package non subclass YES
4 Different package subclass NO
5 Different package non subclass NO

TCE Online Course – Java programming 3


Private keyword

Private: The private access modifier is specified using the keyword private. The data members, class or methods
which are declared as private are accessible only within the class within which it is declared

[Link] Access control Available


1 Same class YES
2 Same package subclass NO
3 Same package non subclass NO
4 Different package subclass NO
5 Different package non subclass NO

TCE Online Course – Java programming 4


Encapsulation

Encapuslation in java is a process of wrapping code and data together in a single unit.
A fully encapsulated class can be created in java by making all the data members of the class private. Setter and
getter methods can be used to set and get the data in it.
Importance of Encapsulation:
✓ It allows to take full control over the data members
✓ Data hiding shall be achieved since other classes are restricted from accessing the private data members.
✓ Creation of an encapsulated class is quite easier and faster
✓ Encapuslated class is most appropriate for testing purpose.

TCE Online Course – Java programming 5


Protected keyword

Protected: The protected access modifier is specified using the keyword protected. The data members, class or
methods which are declared as protected accessible within the same package or subclasses in different packages

[Link] Access control Available


1 Same class YES
2 Same package subclass YES
3 Same package non subclass YES
4 Different package subclass YES
5 Different package non subclass NO

TCE Online Course – Java programming 6


Public keyword

Public: The public access modifier is specified using the keyword public.. The data members, class or methods
which are declared as public accessible from everywhere in the program. There is no restriction on the scope of
public data members.

[Link] Access control Available


1 Same class YES
2 Same package subclass YES
3 Same package non subclass YES
4 Different package subclass YES
5 Different package non subclass YES

TCE Online Course – Java programming 7


TCE Online Course – Java programming 8

You might also like