Access modifiers are used to implement an important aspect of OOP known as Data Hiding.
There are four types of access modifiers in Java.
1. Public
2. Private
3. Protected
4.Default
Note -> If we do not specify any access modifiers for the members inside the class then by default the access modifier for the member will be Private.
1. Public : The class members which are declared under the public specifier will available to everyone and can be accessed by other classes and functions too.
The public members of a class can be accessed from anywhere in the program using direct member access operator (.) with the object of that class.
//Program to demostrate public access modifier.
package com.company;
//class definition
class circle
{
public:
double radius;
double compute_area()
{
return 3.14*radius*radius;
}
};
//main function
public class Main{
public static void main(String[]args)
{
circle obj;
//accessing public data_member outside class
obj.radius = 5.5;
System.out.println("Radius is :" obj.radius "n");
System.out.println("Area is :"obj.compute_area());
}
}
In the above program the data member radius is declared as public so it could be accessed outside the class and thus was allowed access from inside main().
2. Private : The class member declared as private can be accessed only by the member functions inside the class. They are not allowed to be accessed directly by any object or function outside the class.
Only the member functions or the friend functions are allowed to access the private data members of a class.
//Program to demostrate public access modifier.
package com.company;
import java.util.Scanner;
//class definition
public class circle
{
public:
double radius;
double compute_area()
{
return 3.14*radius*radius;
}
};
//main function
public static void main(String[]args)
{
circle obj;
//accessing public data_member outside class
obj.radius = 5.5;
System.out.println("Radius is :"obj.radius"n");
System.out.println("Area is :"obj.compute_area());
}
The output of the above program is a compile time error because we are not allowed to access the private data members of a class directly outside the class.
Yet as access to obj.radius is attempted, radius being a private data member we obtain a compilation error.
However, we can access the private data members of a class indirectly using the public member functions of the class.
/*Program to demonstrate private access data members of a class
indirectly using the public member functions of the class.*/
pacakge com.company;
import java.util.Scanner;
//class definition
public class circle
{
//private data member
private:
double radius;
//public data member
public:
void compute_area(double r)
{
//member function can access private data members
radius = r;
double area = 3.14*radius*radius;
System.out.println("Radius is :"radius);
System.out.println("Area is :"area);
}
};
//main function
public static void main(String[]args)
{
circle obj; //creating object of the class
obj.compute_area(1.5);
3. Protected : Protected access modifier is similar to private access modifier in the sense that it can’t be accessed outside of its class unless with the help of friend class, the difference is that the class members declared as protected can be accessed by any subclass(derived class) of that class as well
//Program to demostrate protected access modifier.
package com.company;
import java.util.Scanner;
//base class
public class parent
{
//protected data members
protected:
int id_protected;
};
//sub class or derived class from public base class
class child:public parent
{
public:
void SetId(int id)
{
/*child class is able to access the inherited protected
data members of base class*/
id_protected = id;
}
void displayId()
{
System.out.println("id_protected is :"id_protected);
}
};
//main function
public static void main(String[]args)
{
child obj;
/*member function of the derived class can access the protected
data member of the base class*/
obj.SetId(81);
obj.displayId();
}