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

Modifiers of Java

The document discusses Java access modifiers and their usage. It explains that there are 4 access modifiers in Java - private, default, protected, and public - which control the visibility and accessibility of classes, interfaces, and class members. Private restricts access to only within the class, default allows access within the same package, protected allows access within the package and subclasses outside the package, and public allows unrestricted access. It provides examples of how each access modifier functions and their increasing order of visibility. It concludes with assignment questions related to access modifiers.

Uploaded by

NANDINI B
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Modifiers of Java

The document discusses Java access modifiers and their usage. It explains that there are 4 access modifiers in Java - private, default, protected, and public - which control the visibility and accessibility of classes, interfaces, and class members. Private restricts access to only within the class, default allows access within the same package, protected allows access within the package and subclasses outside the package, and public allows unrestricted access. It provides examples of how each access modifier functions and their increasing order of visibility. It concludes with assignment questions related to access modifiers.

Uploaded by

NANDINI B
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Pg1

Thursday, June 25, 2020 1:07 PM

Modifiers :

1. all modifiers in java is a keyword.


2. In java, we can classify modifiers into 2

a. access modifiers / access specifiers


b. non-access modifiers

a. Access Modifiers / Access Specifiers :


it is a keyword, which changes the visibility (scope ) of the member
of java.

Different levels of Access Modifiers in java :

1. private
2. default
3. protected
4. public

Modifiers Of Java Page 1


Modifiers Of Java Page 2
Pg2
Thursday, June 25, 2020 1:41 PM

Usage of a member? private default / protected public


package scope
1. within the same class ? YES YES YES YES
2. different class same NO YES YES YES
package ?
3. different class outside a NO NO NO YES
package ?
4. with the help of subclass NO YES YES YES
present in the same
package ?
5. with the help of subclass NO NO YES YES
present outside the package

Private default protected public


class NO YES NO YES

interface NO YES NO YES

Global YES YES YES YES


variables
methods YES YES YES YES

construct YES YES YES YES


ors
initializer Not Allowed not Allowed Not Allowed Not Allowed
s

private :
it is a keyword, an access modifier.
any member of a class prefixed with private can
be used only within the same class, we cannot
use a private member directly anywhere else.

For example, Refer: app20/private/src

Modifiers Of Java Page 3


default:
In a java class, if a member of a class, is not
prefixed with any of the access modifier then it
is considered to be a default access modifier. It
is also known as package scope, because the
member with default access can be used
anywhere within the same package but it cannot
be used anywhere outside the package.

For example, Refer: app20/default/src


protected:
It is a keyword, which represents an access
modifier.
Any member prefixed with protected access can
be used anywhere within the package, it can
also be used outside a package only with the
help of subclass present outside a package.

for example, Refer: app20/protected/src

public :

It is a keyword, which represents an access


modifier.
Any member prefixed with public can be used
anywhere within or outside a package.

Access Modifiers in increasing order of their


Visibility :

private < default < protected < public

Modifiers Of Java Page 4


Modifiers Of Java Page 5
Assignment 1
Friday, June 26, 2020 1:50 PM

1. What is the difference between private and default? explain with example
2. How protected is different from default and public? explain with
example programs.
3. Can we restrict the object creation of a class outside a package ? justify
with an example program.
4. Can we restrict the object creation of a class such that, the object must not
be created anywhere except within the class ? justify with an example
5. How do we use a protected member in a class present outside a package?
explain with an example.

Modifiers Of Java Page 6

You might also like