Monday 1 August 2016

Acess Modifier in java



Access modifiers

The access modifiers in java specify accessibility or scope of a data member, method, constructor, class. There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc.

Types of Access Modifier

There are four types of java access modifiers:
1) Public                                   2) Private        
3) Protected                             4) Default

1) Private access modifier: -

The private access modifier is accessible only within class.

2) Default access modifier: -

If you don't use any modifier, it is treated as default bydefault. The default modifier is accessible only within package.

3) Protected access modifier: -

The protected access modifier is accessible within package and outside the package but through inheritance only. The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class.

4) Public access modifier: -

The public access modifier is accessible everywhere. It has the widest scope among all other modifiers.

Access Modifier Table: -

Access Modifier
within class
within package
outside package by subclass only
outside package
Private
Y
N
N
N
Default
Y
Y
N
N
Protected
Y
Y
Y
N
Public
Y
Y
Y
Y

No comments:

Post a Comment