Use of Modifiers in Java

Modifiers in Java are frequently used inside and outside of class, for various purposes.
In this tutorial we will be describing the modifiers used in Java with examples.

Two categories of modifiers

  • Access modifiers
  • Non access modifiers

What are Access Modifiers?

These can of the following four types

  • Private
  • Public
  • Protected
  • Default

These modifiers are frequently used in the classes and methods and constructors etc.

Private 

Private access modifier is most often used in classes with the instance variables. The purpose of Private access modifier is to hide data of particular from other classes. With use of private keyword before the variable, the variables of the class become private and can be accessed with in the class.

Public:

Public access modifiers are used when we want other classes to access some of the data from our class. Once declared public data is available to other class no restriction. Note only class in the same package can access the data that is declared public.

Protected:

Protected access modifiers make data available to all those classes that are subclass of the current class that is declaring the data to be protected. All the subclasses can access the data from parent classes.

Note only class in the same package can access the data that is declared public.

Default:

Default modifiers is never as keyword instead if class or variable is  not defined with any access modifier then the java will consider will apply default access modifier . it scope is restricted to current package.

Examples:

To be continue. stay tuned to the website.

admin
 

student of computer Science , having interest and experience in C++,Java,Php,Html,Css,JavaScript, wordpress and android development