Which of the following access control modifier is used to access a member of class before the object of that class is created?


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

Java access modifiers are used to provide access control in java. Java provides access control through three keywords - private, protected and public. We are not required to use these access modifiers always, so we have another one namely “default access”, “package-private” or “no modifier”.

Java Access Modifiers

Which of the following access control modifier is used to access a member of class before the object of that class is created?
We can use java access modifiers with Classes as well as Class variables and methods. We are allowed to use only “public” or “default” access modifiers with java classes.

  1. If a class is “public” then we can access it from anywhere, i.e from any other class located in any other packages etc.
  2. We can have only one “public” class in a source file and file name should be same as the public class name.
  3. If the class has “default access” then it can be accessed only from other classes in the same package.

Java Access Modifiers with Class Member

We can have all the four access modifiers for class member variables and methods. However, member access modifier rules get applied after the class level access rules. For example, if a class is having default access then it will not be visible in other packages and hence methods and variables of the class will also be not visible. We will look into each of them separately and then we will show the java access modifiers usage with a simple program.

Java Access Modifiers - public keyword

If a class member is “public” then it can be accessed from anywhere. The member variable or method is accessed globally. This is the simplest way to provide access to class members. However, we should take care of using this keyword with class variables otherwise anybody can change the values. Usually, class variables are kept as private and getter-setter methods are provided to work with them.

Java Access Modifiers - private keyword

If a class member is “private” then it will be accessible only inside the same class. This is the most restricted access and the class member will not be visible to the outer world. Usually, we keep class variables as private and methods that are intended to be used only inside the class as private.

Java Access Modifiers - protected keyword

If class member is “protected” then it will be accessible only to the classes in the same package and to the subclasses. This modifier is less restricted from private but more restricted from public access. Usually, we use this keyword to make sure the class variables are accessible only to the subclasses.

Java Access Modifiers - default access

If a class member doesn’t have any access modifier specified, then it’s treated with default access. The access rules are similar to classes and the class member with default access will be accessible to the classes in the same package only. This access is more restricted than public and protected but less restricted than private. (Least Accessible) private < default < protected < public (Most Accessible) The below table summarise above access modifiers with respect to different classes in the same package or other packages and subclasses.

Which of the following access control modifier is used to access a member of class before the object of that class is created?
Let’s write some simple classes where we will see the java access modifiers in action. TestA.java

package com.journaldev.access;

class TestA {

	public void methodPublic(){
		methodPrivate();
	}
	
	protected void methodProtected(){
		methodPrivate();
	}
	
	void methodDefault(){
		methodPrivate();
	}
	
	private void methodPrivate(){}
}

Note that TestA class has default access and the private class method is accessible to all other parts of the same class. TestB.java


package com.journaldev.access;

import com.journaldev.access.TestA;

public class TestB {

	public static void main(String args[]) {
		new TestA().methodPublic();
		new TestA().methodProtected();
		new TestA().methodDefault();

	}

	public void methodPublic() {

	}

	protected void methodProtected() {

	}

	void methodDefault() {

	}

	private void methodPrivate() {
	}

}

Note that TestB is in the same package as TestA class and hence it is able to access it’s class members. private members are not accessible but all other members are accessible because of the same package. TestC.java


package com.journaldev.access.child;

import com.journaldev.access.TestB;

public class TestC {

	public static void main(String[] args) {
		new TestB().methodPublic();
	}

}

TestB class is accessible because it’s public. Only public members of TestB class is accessible because TestC class is not in the same package nor its subclass of TestB. TestE.java


package com.journaldev.util;

import com.journaldev.access.TestB;

public class TestE extends TestB {

	public static void main(String[] args) {
		new TestB().methodPublic();
		new TestB().methodProtected(); // compile time error

		// works, accessing super class protected method using subclass
		new TestE().methodProtected();

	}

}

Since TestE class is a subclass of TestB, we can access TestB protected members through child class TestE. If we try to access the superclass protected method directly, we will get a compile-time error. That’s all for the java access modifiers, it’s simple to understand. Just don’t confuse with the default and protected access. An easy way to remember is that default access is more restricted than protected and protected members are accessible in subclasses. Recently I made a video to explain java access modifiers in detail, you can watch it below on YouTube. https://www.youtube.com/watch?v=QKjnbC3UBtY

Which of the following is used to access a member of class before object of that class is created?

If no access specifier is used then by default member is public within its own package & cannot be accessed by Java run time system. 2. Which of these is used to access a member of class before object of that class is created? Explanation: None.

Which access modifier is used to access members only to the members of that class?

For members, there are two additional access modifiers: private and protected . The private modifier specifies that the member can only be accessed in its own class.

Which of the following access modifier can be used in order to access the members of a class in another class in the same package?

Protected Access Modifier It is a keyword. This access modifier is used to access the methods or data members of a class within the same package as well as outside the package but only through inheritance.

Which of the following can be the access modifiers for members of the class?

There are four types of Java access modifiers: Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class. Default: The access level of a default modifier is only within the package.