Java Access Modifiers

 What are Access Modifiers?

Access modifiers are used to control the visibility of classes, interfaces, variables, methods, constructors, data members, and setter methods in Java.
Java Access Modifiers
Java Access Modifiers

Java access modifiers are classified into four types:

Private

The access level of a private modifier is only within the class. It cannot be accessed from outside the class.

Default

It cannot be accessed from outside the package. If you do not specify any access level, it will be the default. declarations are visible only within the package (package private).

Protected :

 A protected modifier's access level is within the package and outside the package through a child class. The child class cannot be accessed from outside the package unless it is created. i.e. declarations are visible within the package or all subclasses.

Public

A public modifier's access level is visible everywhere. It may be accessible from inside and outside the class, from within and outside the package, and from within and outside the package. 

Let's look at a basic table to see how access modifiers work in Java.
Java Access Modifiers
Let us explain each of them with an example.

1.Private :

When variables and methods are declared, private they cannot be accessed outside of the class. For example,


Example :
class Test {
// private variable
private String name;
}

public class Main {
public static void main(String[] main){

// create an object of Data
Test t = new Test();

// access private variable and field from another class
t.name = "SomethingString";
}
}


We've declared a private variable named name in the above example. We are accessing these private members from outside the class. when we compile the program so there is a compile-time error occur,


Main.java:13: error: name has private access in Data
t.name = "SomethingString";
^

2.Default :

If no access modifier is explicitly specified for classes, methods, variables, and so on, the default access modifier is used.


Example:
package basicProgram;
class Test
{
void message()
{
System.out.println("This is a message");
}
}

The Test class has the default access modifier in this case. the class is visible to all classes in the basicProgram package. We will get a compilation error if we try to use the Test class in a class other than basicProgram package.


3.Protected :

We can access methods and data members inside the same package as well as from subclasses when they are declared protected. 


Example:
class Car 
{
// protected method
protected void display()
{
System.out.println("This is car");
}
}

class Nano extends Car
{
public static void main(String[] args)
{
// create an object of Nano class
Nano obj = new Nano();
// access protected method
obj.display();
}
}

Output : This is car


We have a protected method named display() within the Car class in the preceding example. The Car class is inherited by the Nano class.

We then created an object obj of the Nano class. Using the object we tried to access the protected method of the parent class. 
Since protected methods can be accessed from the child classes, we are able to access the method of car class from the Nano class.

4.Public :

We can access public methods, variables, classes, and other objects from anywhere if they are declared public. The scope of the public access modifier is unrestricted.


Example:
//Car.java file
// public class
public class Car
{
// public variable
public int WheelCount;
// public method
public void display()
{
System.out.println("This is a Car.");
System.out.println("This Car has " + WheelCount + " Wheels.");
}
}

// Test.java file
public class Test
{
public static void main( String[] args )
{
// accessing the public class
Car carobj = new Car();
// accessing the public variable
carobj.WheelCount = 4;
// accessing the public method
carobj.display();
}
}

Output : This is a Car.
This Car has 4 Wheels.

The public class Car is accessed from the Test class.
The public variable WheelCount is accessed from the Test class.
The public method display() is accessed from the Test class.


Tech Smart

Hello, my name is Sam and I am the blog's author. I enjoy blogging since I am passionate about it and I am an enthusiastic learner. Blogging provides me a greater platform to share all of my knowledge and experiences with my readers.

Post a Comment

if you have any doubts, Please let me know

Previous Post Next Post