JavaWorking with Modifiers in Java

Working with Modifiers in Java

Java Developer Tutorials

In this programming tutorial, we will take a look at how to work with modifiers in Java. Modifiers in Java are keywords used to change the behavior of a class or method. We will discuss the different types of modifiers and how to use them in Java applications.

Want to learn Java in a classroom or online course setting? We have a tutorial covering the Best Online Courses to Learn Java to help get you started.

What are Modifiers in Java?

A Java modifier is used to control the visibility of an interface, class, method, or a variable. Modifiers in Java can be either access modifiers or non-access modifiers. Using access modifiers, you can control which classes, methods, or variables are visible. These keywords can be used to restrict access to classes and members, either from within the same package or from other packages.

Further, modifiers are keywords that you can add to declarations and definitions in order to change their behavior. When deciding which modifier to use, consider whether the element should be accessible from outside the class, whether it should be a class member or an instance member, and whether it should be able to be changed.

Access modifiers in Java are of three types: public, protected, and private.

  • Public: Classes and methods marked as public can be accessed by any other classes in another package. They may also be accessed by subclasses in the same package.
  • Protected: Classes and methods marked as protected can be accessed only by subclasses in the same package (i.e., they are only visible within their defining class).
  • Private: Private members cannot be accessed outside of their defining class or interface.

What are the Benefits and Downsides of Java Modifiers?

One of the key benefits of using modifiers in Java is that they can help make code more readable. For example, if you have a method that is only supposed to be called from within the same class, you can use the private modifier to make that clear.

This can help other developers who are reading your code to understand your intentions more easily. Another pro of using modifiers is that they can help prevent bugs. For example, if you mark a method as final, that means it can not be overridden by a child class.

On the other hand, one of the downsides of using modifiers is that they can add complexity to your code. For example, if you use too many modifiers, it can be difficult for other developers to understand your code. Additionally, if you use modifiers incorrectly, it can lead to errors in your code.

Read: How to Work with Constructors in Java

What are the Types of Modifiers in Java?

As stated previously, modifiers are keywords that you can use to change the behavior of a class or method. In this section, we will go through the several types of modifiers and how to utilize them. Here are the types of modifiers supported by Java:

  • Access modifiers: public, private, and protected.
  • Non-access modifiers: abstract, static, final, volatile, and transient.

Access Modifiers in Java

Access modifiers control who can access a class or method. In Java you can have three access modifiers such as, public, private, and protected.

Public modifiers allow code in any class or subclass to access the class or method. The following code listing illustrates how you can work with public modifiers in Java:

class Product {
    public int id;
    public String productCode;
    public String productName;
}
public class Demo {
    public static void main(String[] main){
        Product obj = new Product();
        obj.id = 1;
        obj.productCode = "P0001";
        obj.productName = "Lenovo Thinkpad laptop";
    }
}

Private modifiers only allow code in the same class to access the class or method, as demonstrated in the following Java code example:

class Author {
    private String firstName;
    private String lastName;
}
public class Demo {
    public static void main(String[] main){
        Author obj = new Author();
        obj.firstName = "Michael";
    }
}

Since the data members of the Author class are private, when you compile the preceding piece of code, the compiler will flag an error.

Protected modifiers, when used on a class or a method, enables code in the same package or subclasses to access the class or method. You can access a protected class or a method in a different package as well using subclasses.

The following code listing shows how you can define a protected method in a base class and then call it using an instance of the derived class in Java:

class Person {
    protected void display() {
        System.out.println("I'm inside the base class.");
    }
}
public class Employee extends Person {
    public static void main(String[] args) {
        Employee obj = new Employee();
        obj.display();
    }
}

Non-Access Modifiers in Java

Non-access modifiers are used to change the behavior of a class or method. The five non-access modifiers are final, abstract, static, volatile, and transient.

  • Final: A final modifier restricts you to extend a class or method that has been decorated with the final modifier. Subclasses of a base class cannot override a final method.
  • Abstract: An abstract modifier can be used on both a class as well as a method to mark them as abstract. You can extend or inherit an abstract class but you cannot create an instance of it. An abstract method is defined as a method that has its declaration in an abstract class but it is defined elsewhere – in the subclasses of the abstract base class.

The following code example shows how you can define an abstract class in Java:

abstract class Vehicle {
    abstract void run();
}
public class Car extends Vehicle {
    void run() {
        System.out.println("I'm inside the car class...");
    }
    public static void main(String args[]) {
        Vehicle obj = new Car();
        obj.run();
    }
}
  • Static: A static modifier specifies the scope of a variable or a method as the class rather than an instance. This modifier is used to associate a member of a class with the class itself instead of an instance of the class.
  • Volatile: A volatile modifier suggests that data can change unexpectedly at any time, so variables marked as volatile should never be cached.
  • Transient: A transient modifier in Java is used to prevent a data member of a class from being serialized.

You can apply modifiers to classes, interfaces, fields, methods, and constructors. Modifiers in Java can also be combined. For example, a field can be both static and final. A method can be public and abstract. A class can be both public and abstract.

Final Thoughts on Java Modifiers

Access modifiers in Java can help you encapsulate members of a class from the external world. In this programming tutorial, we examined how to work with modifiers in Java, including how to declare variables and methods as private, protected, or public. Happy reading!

Read more Java programming tutorials and software development tips.

Latest Posts

Related Stories