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