Java Access Modifiers

Java access modifiers

Java supports four access modifiers that you can use to define the visibility of classes, methods, and attributes. Each of them specifies a different level of accessibility and can only use one modifier per class, method or attribute. As a general rule, you should always use the most restrictive modifier that still allows you to implement your business logic.

The Java access modifiers are,

  • private
  • No modifier
  • protected
  • public

Let’s take a look at each of these modifiers and discuss when you should use them.

Private

This is the most restrictive and most commonly used access modifier. If you use the private modifier with an attribute or method, you can only access it in the same class. Subclasses or any other class in the same package or in a different package cannot access this attribute or method.

As a general rule, the private modifier must be your default option for all internal attributes and methods that should not be called external classes. You may have to make an exception to this rule when using inheritance and some subclasses need direct access to an internal attribute or method. In this case, you must use the protected switch instead of private.

Example

//Java program to illustrate error while
//using class from different package with
//private modifier
package p2;

class A
{
private void display()
{
System.out.println(“Besanttechnologies”);
}
}

class B
{
public static void main(String args[])
{
A obj = new A();
//trying to access private method of another class
obj.display();
}
}

Output

error: display() has private access in A
        obj.display();

In the above example, we have create two classes A and B within same package p2. We will declare a method in class A as private and try to access this method from class B .

No modifier

When you do not provide any access modifier to your attribute or method, you can access it in your class and in all classes in the same package. That’s why it’s often called a private package.

This may seem a bit confusing at first, but it is very useful when the classes in your package implement a well-defined set of logic and you want to control the API that is available for classes outside that package. You can then use the visibility of the package to implement a method that can only be used by classes within this package. This allows you to create an internal package and an external API.

Example

//Java program to illustrate default modifier
package p2;

//Class besant is having Default access modifier
class besant
{
void display()
{
System.out.println(“Hello World!”);
}
}

//Java program to illustrate error while
//using class from different package with
//default modifier
package p3;
import p2.*;

//This class is having default access modifier
class besantNew
{
public static void main(String args[])
{
//accessing class besant from package p2
besanttechnologies obj = new besant();

obj.display();
}
}

Output

Compile time error

In the above example, we have created two packages and the classes in the packages will be having the default access modifiers and we will try to access a class from one package from a class of second package.

Interested in learning Java? Enroll now:” Java training in Chennai “

Protected

The attributes and methods with the protected access modifier can be accessed within their class, by all classes within the same package and by all subclasses within the same or other packages.

The protected modifier is mainly used for internal methods that must be called or replaced by subclasses. You can also use it to allow subclasses to access the internal attributes of a superclass directly.

Example

//Java program to illustrate
//protected modifier
package p2;

//Class A
public class A
{
protected void display()
{
System.out.println(“Besant Technologies”);
}
}

//Java program to illustrate
//protected modifier
package p3;
import p2.*; //importing all classes in package p2

//Class B is subclass of A
class B extends A
{
public static void main(String args[])
{
B obj = new B();
obj.display();
}

}

Output

Besant Technologies

Interested in learning Java? Enroll now:” Java training in Bangalore

Public

This is the least restrictive access modifier. The methods and attributes that the publicmodifier uses can be accessed within its current class and for all other classes.

Public methods and attributes become part of the public API of your class and any component in which you include them. This is almost never a good idea for any attribute, and you should think twice before using this modifier in a method.

If a method is publicly available, it is necessary to ensure that it is well documented and that it is all input values. Also, remember that, sooner or later, this method will be used by some part of your application that will make it difficult to change or eliminate it.

In general, your public API should be as simple as possible and include only the methods that should be used by other parts of the application or by external clients.

Example

//Java program to illustrate
//public modifier
package p1;
public class A
{
public void display()
{
System.out.println(“Besant Technologies”);
}
}
package p2;
import p1.*;
class B
{
public static void main(String args[])
{
A obj = new A;
obj.display();
}
}

Output

Besant Technologies

To getting expert-level training for Java Training in your location – java training in Chennai | java training in Bangalore  | java training in Pune | java training in Chennai | java training in Bangalore | java training in Chennai | java interview questions and answers |  core java interview questions and answers | java training in Chennai | For getting java online training | java online training

Leave a Comment