Modifiers in Java

Modifiers are key words in java that is used to restrict or add access level for classes, attributes, methods and constructors.

Access Modifiers - default, Private, Protected, Public
Non Access modifiers
      For Classes – final, abstract
      For Methods or attributes – final, static, abstract, transient, synchronized, volatile

Access Modifiers Visibility
Access Modifiers Visibility

1. 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.

Scope – within same package

2. Private

The private access modifier is specified using the keyword private.
The methods or data members declared as private are accessible only within the class in which they are declared.
Any other class of same package will not be able to access these members.


Scope – within same class

3. Protected

The protected access modifier is specified using the keyword protected.
The methods or data members declared as protected are accessible within same package or sub classes in different package.

Scope – within same package and other package using inheritance

4. Public

The members, methods and classes that are declared public can be accessed from anywhere. This modifier doesn’t put any restriction on the access.

Scope – accessible from anywhere inside or outside of the package.

To get more details please watch below youtube video and Subscribe the channel.



No comments:

Post a Comment