Access modifiers in Java
Modifiers are keywords used to define the scope and behavior of the classes, methods and variables. Class definitions access levels and this determine where and this determines where an object of the class can be instantiated. All the variables and methods in a class are available to any code written for that class.
There are four possible access specifiers : public, protected , private and default access. The public access refers to data members and methods that can be freely accessed code from any other class. The protected access refers to the members that can't be accessed by code from any other class, except the class that inherits from these class and classes in the same package _ which refers to the collection of classes ,basically all the classes store in the same directory. The private access means that the member will not be accessed by code in any otheyckass, including classes that inherit from this one. Then there is the fourth possibility, the default access, which is used if there is no access specifier mentioned.
There are other modifiers which can be used in a program. The static modifier is used to create methods and variables, the abstract modifier is used to create abstract class and methods, the final modifier for finalizing the implications of a classes, methods and variables the synchronized and volatile modifiers for threads and native modifiers for creating native variable and methods .
1.private
2.public
3.protected
4.default
1.private: private members of class can be access only same class. Private members can not be access in sub class. We can also declare private variables and methods.
Syntax : private int a;
Example Class sample
{
Public static void main(stringargs[])
{
Private int a;
Void show()
{
A=10;
}
}
}
2.public : public members of can be access of any other class. We can access public members out side the package. We can declare public members with public keyword.
We can declare public variables and methods.
Syntax public int a;
Example.
Class A
{
Public int a=10,b=20,c;
Public
Void sum()
{
C=a+b;
System. Out. Println(c);
}
}
Class B
{
Public static void main(string arg[])
{
A ob=new ob();
On. Sum ();
}
}
3. Protected: protected members can be access in sub class, same package and other package in sub class. It can't be access other package non sub class.
We can declare protected members with protected keyword.
Syntax : protected int a;
Example
Package p1;
Class A
{
Public static void main(string arg[])
Protected int i=10 j=20;,K;
Void sum()
{
K=i+j;
}
}
}
Package p2;
Important p1. A;
Class sample
{
Public static void main(string arg[])
{
Into i=10;
Into j=20;
}
}
4. Default. We can access default members same class, sub class and same package. Normally default members visibility only package level
Syntax: int a;
Example
Package OK;
Class A
{
Int a=10,b=20,c;
Void sum()
{
C=a+b;
System.out.println(c);
}
}
Class B
{
Public static void main(srring arg[])
{
A ob=new A();
Ob. A=10;
Ob. b=20;
}
}