Which of the following keyword is used to restrict the accessibility of a method within the enclosing class?

A modifier in Java is used to define the accessibility and behaviors of the classes, their constructors, fields, and methods. There are two types of modifiers in Java, access and non-access. The access modifiers define the accessibility whereas the non-access modifiers report the behavioral information of classes, constructors, and methods to the JVM. This article provides a detailed guide on modifiers in Java with the following learning outcomes:

  • types of modifiers
  • how modifiers work in Java

Types of modifiers

This section considers the modifier types in detail, additionally, the sub-types of each type are also described.

Access Modifiers

These modifiers are responsible for defining the accessibility of the classes, constructors, method, etc. For this, the access modifiers are further divided into several sub-types.

Default: This default access modifier is assigned when no other modifier is used. The members of a default class are accessible within the package where the class resides.

Which of the following keyword is used to restrict the accessibility of a method within the enclosing class?

Public: This is the most used and common access modifier. The methods or data members are declared using public keywords and can be accessed from anywhere without any restriction.

Which of the following keyword is used to restrict the accessibility of a method within the enclosing class?

Note: The main class can be created using the default or public keywords. However, the methods/constructors/attributes can be created as private, protected, default, and public

Private: The data members of the private class are declared using private keywords and are accessible only within the parent class. Moreover, any other method from any other class cannot access the members of the private class. As main class cannot be declared using a private keyword, so, the nested classes or methods practice the private keyword.

Protected: Members of a package with a protected access modifier are accessible within the same package of the same subclass.

Non-Access Modifiers

The JVM uses these modifiers to find out how classes behave. The following types of non-access modifiers can be used:

Final: The final non-access modifiers are used with the classes to restrict their inheritance. The final keyword is used with the classes to define their un-inherited behavior and JVM then does not allow any subclass to extend such kind of class. Similarly, the methods and variables can also be used with the final non-access modifiers to restrict their behavior.

Which of the following keyword is used to restrict the accessibility of a method within the enclosing class?

Abstract: The abstract classes are only inherited not instantiated. The primary purpose of the abstract classes is to extend them to make use of these classes. Moreover, the abstract methods are also like abstract classes, they are declared in a superclass, but they are instantiated in the subclass at the time of extending the sub-class.

Which of the following keyword is used to restrict the accessibility of a method within the enclosing class?

Note: The final and abstract keywords can be used to define the main class. The methods and attributes can be defined using the non-access modifiers like static, transient, synchronized.

  • Synchronized: The synchronization concept comes into action where multiple programs are executed to minimize resource consumption. The synchronized non-access modifier helps in restricting one method to access by multiple threads. This non-access modifier is only applicable to methods.
  • Static: This non-access modifier is applicable to the inner classes, variables, methods. In the static classes, variables are associated with the class, and any change to variables is distributed to all the objects. Moreover, the static methods can access static variables or other static methods of the same class.
  • Native: The native keyword is used to identify that the method is implemented in native code using Java Native Interface. Usually, the methods implemented in C/C++ are considered as the native methods.
  • Transient: The transient non-access modifier is used with the class names and is practiced to secure receiving of data over the network. The transient keyword is used with the class names and if the class is transient then the data members need not to transient. You would have to use transient keywords with the data member that does not need serialization.
  • Strictfp: It stands for strict floating-point and restricts the floating-point calculations to ensure the result is the same across various platforms. The floating-point calculations are platform-dependent and return different results on various platforms.

Conclusion

The modifiers in Java can be of either access or non-access type. Data members and methods of a class are made accessible by access modifiers. Whereas the non-access modifiers notify JVM about the behavior of data members/methods of a class. This informative post describes the access and non-accessmodifiers along with their types/subtypes in Java. After going through this post, you would have learned to choose the appropriate modifiers for your class members in Java.

Which of the following keyword is used to restrict the accessibility of a method within the enclosing class?

Overview of Protected Keyword in Java

Protected keywords are keywords that are used to restrict the scope within which the variable, method, and constructors can be accessed. It is one of the types of access modifiers in Java. They are used to differentiate between the scope of methods, variables, constructors, and classes. There are 4 types of access modifiers in Java, and they are:

  1. Default keyword: They can be accessed only within the package and cannot be called outside it. As the name suggests, it is automatically assigned as the default when no access specifier is mentioned.
  2. Public keyword: They can be accessed from anywhere in the program. This means it can be used from the same or different classes and the same or a different package.
  3. Private keyword: They restrict the keywords to a higher level by not allowing them to be accessed from anywhere outside the class itself.
  4. Protected keyword: In this article, we shall get to know more about protected keywords.

Once a variable or a method is marked as protected, it can only be accessed by the below methods:

  • Inside the same class in which it is declared.
  • From other classes which are also in the same package as the declared class.
  • Classes inherited from the declared one, irrespective of their package.

Protected keywords are like a combination of both public and private keywords since they were introduced to access the variables outside the class (which is not possible in the case of private keywords) and maintain that only certain methods can inherit the same.

Syntax

Protected keywords are declared with the keyword prefixed to them as “protected”. We first declare the protected keyword in one of the class called “MyClass” as below:

class MyClass { protected String name = "Katy"; protected int token= 55; } public class SubClass extends MyClass { public static void main(String[] args) { SubClass obj = new SubClass(); System.out.println(obj.name + "'s token number is: " + obj.token); } }

Here the class “SubClass” extends “MyClass”, and hence the protected keyword can be used here by creating an object of SubClass and by calling the variables.

Output:

 

Which of the following keyword is used to restrict the accessibility of a method within the enclosing class?

Protected keywords can only be used at the member level, i.e. inner classes declared outside of a function and non-static. Protected keyword is different from that of private as they can be accessed outside of a class and in the subclass of another package.

Some of the restrictions on using protected keywords are:

  • They cannot be used for declaring classes as protected.
  • Interfaces cannot be declared as protected.
  • Accessibility outside the package is only through inheritance.
  • A constructor that is made protected cannot be accessed outside the package by creating its instance.

Examples of Protected Keyword in Java

Let us go through some examples where we can understand the concept of protected keywords better.

1. Calling protected keyword without extending the parent class

Here we try to call the keyword from the parent class of “package1”. “ProtectedExample2” is created in “package2”, and the keyword “disp” is called here. But the code will not be able to access the keyword since the child class has not inherited its value from the main class and will throw an exception as shown.

Code:

package com.package1; public class Example { protected String disp="Printing message from protected variable from package1"; } //Create new package as com.package2 //Create new class as ProtectedExample2 package com.package2; import com.package1.Example; public class ProtectedExample2 { public static void main(String[] args) { ProtectedExample2 a=new ProtectedExample2(); System.out.println(a.disp); } }

Output:

Which of the following keyword is used to restrict the accessibility of a method within the enclosing class?

2. Accessing a protected class

In this example, we try to access the class “ProtectedExample5”, which is protected. This causes a compilation error.

Code:

protected class ProtectedExample5 { void display() { System.out.println("Try to access outer protected class"); } public static void main(String[] args) { ProtectedExample5 p=new ProtectedExample5(); p.display(); } }

Output:

Which of the following keyword is used to restrict the accessibility of a method within the enclosing class?

3. Displaying protected keyword from the same package but different class

In the below example, we first create a package called “com.package1” and create a new class by the name “Example”. Here we declare our keyword “disp” is protected. We shall try to display this protected keyword using the class “Example1”. For this, first, an object of parent class “Example1” needs to be created and then print the value assigned to the keyword “disp”.

Code:

package com.package1; public class Example { protected String disp="Printing message from protected variable from package1"; } class Example1 { public static void main(String[] args) { Example obj=new Example(); System.out.println(obj.disp); } }

Output:

Which of the following keyword is used to restrict the accessibility of a method within the enclosing class?

4. Displaying protected keyword from a different package

Using the same code as above, we shall see how to call the protected keyword by creating a different package, “package2”. A protected keyword can be accessed only through inheritance from package1; hence “ProtectedExample2” is extended from “Example”. In a similar way as the first example, we have to create an object of the class “ProtectedExample2” in order to access the protected keyword from package “com.package1”.

Code:

package com.package2; import com.package1.Example; public class ProtectedExample2 extends Example{ public static void main(String[] args) { ProtectedExample2 a=new ProtectedExample2(); System.out.println(a.disp); } }

Output:

Which of the following keyword is used to restrict the accessibility of a method within the enclosing class?

5. Accessing a protected class by overriding to sub-class

Here the class is declared as protected inside the inherited class “Example5”. Also, a protected class called “Example” is declared outside the function but in the same package. When an object of “Example5” is created and the protected class “disp()” is called, we can observe that the overridden method is called instead of the outside class. This is because we shall not be able to import “com.package1” and its class “Example” since it is not visible and causes a compilation error.

Code:

//Create a file by Example.java package com.package1; class Example { protected void disp() { System.out.println("Printing from protected class in the outside function"); } } //Create a class by the name Example5.java public class Example5 extends Example { protected void disp() { System.out.println("Accessing the overriden function"); } public static void main(String[] args) { Example5 exp=new Example5(); exp.disp(); } }

Output:

Which of the following keyword is used to restrict the accessibility of a method within the enclosing class?

Importance of Protected Keyword

The importance of protected keyword in java is:

  • These keywords allow the classes or their variables to be inherited from their parent class which is not possible with any other restricted keyword such as private.
  • A protected keyword is the combination of a private keyword’s advantage and that of a public keyword. It eliminates the disadvantage of public keyword that the variable or class be accessible from anywhere in the program by restricting the scope.

Conclusion

As shown in the above examples, we choose protected keywords depending on the access level we require at the code level. They help greatly in cases where the same variable or class needs to be accessed from other inherited methods in the program. A parent-child relationship is always present between the parent class and its sub-classes which are using the protected keyword.

This is a guide to Protected Keywords in Java. Here we discuss the overview, different examples of protected keywords in java, along with importance. You may also look at the following articles to learn more –