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 modifiersThis section considers the modifier types in detail, additionally, the sub-types of each type are also described. Show Access ModifiersThese 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. 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. 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 ModifiersThe 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. 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. 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.
ConclusionThe 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.
Overview of Protected Keyword in JavaProtected 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:
Once a variable or a method is marked as protected, it can only be accessed by the below methods:
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. SyntaxProtected 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:
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:
Examples of Protected Keyword in JavaLet us go through some examples where we can understand the concept of protected keywords better. 1. Calling protected keyword without extending the parent classHere 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: 2. Accessing a protected classIn 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: 3. Displaying protected keyword from the same package but different classIn 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: 4. Displaying protected keyword from a different packageUsing 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: 5. Accessing a protected class by overriding to sub-classHere 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: Importance of Protected KeywordThe importance of protected keyword in java is:
ConclusionAs 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. Recommended ArticlesThis 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 – |