Which of the following modifier is used to specify whether a member is a class member or an instance


Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors. The four access levels are −

  • Visible to the package, the default. No modifiers are needed.
  • Visible to the class only (private).
  • Visible to the world (public).
  • Visible to the package and all subclasses (protected).

Default Access Modifier - No Keyword

Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.

A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.

Example

Variables and methods can be declared without any modifiers, as in the following examples −

String version = "1.5.1"; boolean processOrder() { return true; }

Private Access Modifier - Private

Methods, variables, and constructors that are declared private can only be accessed within the declared class itself.

Private access modifier is the most restrictive access level. Class and interfaces cannot be private.

Variables that are declared private can be accessed outside the class, if public getter methods are present in the class.

Using the private modifier is the main way that an object encapsulates itself and hides data from the outside world.

Example

The following class uses private access control −

public class Logger { private String format; public String getFormat() { return this.format; } public void setFormat(String format) { this.format = format; } }

Here, the format variable of the Logger class is private, so there's no way for other classes to retrieve or set its value directly.

So, to make this variable available to the outside world, we defined two public methods: getFormat(), which returns the value of format, and setFormat(String), which sets its value.

Public Access Modifier - Public

A class, method, constructor, interface, etc. declared public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class belonging to the Java Universe.

However, if the public class we are trying to access is in a different package, then the public class still needs to be imported. Because of class inheritance, all public methods and variables of a class are inherited by its subclasses.

Example

The following function uses public access control −

public static void main(String[] arguments) { // ... }

The main() method of an application has to be public. Otherwise, it could not be called by a Java interpreter (such as java) to run the class.

Protected Access Modifier - Protected

Variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.

The protected access modifier cannot be applied to class and interfaces. Methods, fields can be declared protected, however methods and fields in a interface cannot be declared protected.

Protected access gives the subclass a chance to use the helper method or variable, while preventing a nonrelated class from trying to use it.

Example

The following parent class uses protected access control, to allow its child class override openSpeaker() method −

class AudioPlayer { protected boolean openSpeaker(Speaker sp) { // implementation details } } class StreamingAudioPlayer extends AudioPlayer { boolean openSpeaker(Speaker sp) { // implementation details } }

Here, if we define openSpeaker() method as private, then it would not be accessible from any other class other than AudioPlayer. If we define it as public, then it would become accessible to all the outside world. But our intention is to expose this method to its subclass only, that’s why we have used protected modifier.

Access Control and Inheritance

The following rules for inherited methods are enforced −

  • Methods declared public in a superclass also must be public in all subclasses.

  • Methods declared protected in a superclass must either be protected or public in subclasses; they cannot be private.

  • Methods declared private are not inherited at all, so there is no rule for them.

java_modifier_types.htm

Which of the following modifier is used to specify whether a member is a class member or an instance

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.