Access Modifiers
Access modifiers are an integral part of object-oriented programming. Access modifiers are keywords used to specify the declared accessibility of a member or a type.They support the function of encapsulation which promotes the idea of hiding functionality.
Types Of Access Modifiers
Public access modifier :
It gives access and visible to all memebers of classes and projects. There are no restrictions on accessing public members.
Example: public static void main(String[] arguments) {
// ...
}
Private access modifier:
It gives access to members of current class. Access is limited to within the class definition. This is the default access modifier type if none is formally specified.
Example:
Class A
Private int Data= 10;
Private void msg()
{
System.out.println(“Hello World”);
}
Public class B
{
Public static void main(String args[])
{
A obj = new A;
System.out.println(obj.data);
Obj.msg(); //compile time error
}
}
Here we created two classes A and B.class A contains private data member and private method.we are accessing these private members from outside the class.so there is compile time error.
Protected access modifier:
It gives access only to members of current class as well as to members of derived class. Access is limited to within the class definition and any class that inherits from the class.
Example:
class Studentdetail
{
protected boolean rollnumber(number rp)
{
// implementation details
}
}
class Streaming Studentdetail {
boolean rollnumber(number rp) {
// implementation details
}
The following parent class uses protected access control, to allow its child class override rollnumber () method:
Here if we define rollnumber () method as private then it would not be accessible from any other class other than Studentdetail. If we define it as public then it would become accessible to all the outside world. But our intension is to expose this method to its subclass only, thats why we used protected modifier.
Internal access modifier:
It gives access only to members of current project or in current namespace. Access is limited exclusively to classes defined within the current project assembly.
Protected Internal access modifier:
It gives access only to members of current project or in current namespace and also to members of derived class.
Access modifiers are an integral part of object-oriented programming. Access modifiers are keywords used to specify the declared accessibility of a member or a type.They support the function of encapsulation which promotes the idea of hiding functionality.
Types Of Access Modifiers
Public access modifier :
It gives access and visible to all memebers of classes and projects. There are no restrictions on accessing public members.
Example: public static void main(String[] arguments) {
// ...
}
Private access modifier:
It gives access to members of current class. Access is limited to within the class definition. This is the default access modifier type if none is formally specified.
Example:
Class A
Private int Data= 10;
Private void msg()
{
System.out.println(“Hello World”);
}
Public class B
{
Public static void main(String args[])
{
A obj = new A;
System.out.println(obj.data);
Obj.msg(); //compile time error
}
}
Here we created two classes A and B.class A contains private data member and private method.we are accessing these private members from outside the class.so there is compile time error.
Protected access modifier:
It gives access only to members of current class as well as to members of derived class. Access is limited to within the class definition and any class that inherits from the class.
Example:
class Studentdetail
{
protected boolean rollnumber(number rp)
{
// implementation details
}
}
class Streaming Studentdetail {
boolean rollnumber(number rp) {
// implementation details
}
The following parent class uses protected access control, to allow its child class override rollnumber () method:
Here if we define rollnumber () method as private then it would not be accessible from any other class other than Studentdetail. If we define it as public then it would become accessible to all the outside world. But our intension is to expose this method to its subclass only, thats why we used protected modifier.
Internal access modifier:
It gives access only to members of current project or in current namespace. Access is limited exclusively to classes defined within the current project assembly.
Protected Internal access modifier:
It gives access only to members of current project or in current namespace and also to members of derived class.