Last Updated on April 12, 2023 by Prepbytes
In Java, access specifiers are used to defining the visibility and accessibility of class members such as variables, methods, and inner classes. Java has four access specifiers: public, private, protected, and default (also known as package-private). The following table shows the scope of each access specifier in Java
Access Specifier | Within Class | Within Package | Outside Package by Subclass only | Outside Package |
---|---|---|---|---|
Private | Y | N | N | N |
Default | Y | Y | N | N |
Protected | Y | Y | Y | N |
Public | Y | Y | Y | Y |
Let us learn about the Protect Access Specifier in Java along with its syntax and examples.
Protected Access Specifier
The protected access specifier in Java allows members to be accessed within the same class, subclasses, and classes in the same package. This means that protected members can be accessed by the class itself, its subclasses (even if they are in a different package), and other classes in the same package. However, protected members cannot be accessed by classes in a different package that are not subclasses of the class containing the protected member.
Syntax for Protected Access Specifier in Java
The syntax for using the Protected Access Specifier in Java with variables and methods is given below.
-
Protected Access Specifier in Java with Variables:
protected data_type variable_name;
-
Protected Access Specifier in Java with Methods:
protected return_type method_name(parameters) { // Protected method // Method body }
Examples of Protected Access Specifier in Java
Now, let’s take a look at some examples to better understand the usage of protected access specifier in Java.
Example 1 of Protected Access Specifier in Java
Accessing Fields specified with Protected Access Specifier within the subclass in the same package.
Code:
class Animal { protected String name; protected void makeSound() { System.out.println("Animal makes sound"); } } class Dog extends Animal { void setName(String newName) { name = newName; // Accessing protected member in the subclass } void makeAnimalSound() { makeSound(); // Accessing protected method in the subclass } } class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.setName("Buddy"); // Setting the name using protected member dog.makeAnimalSound(); // Accessing the protected method System.out.println("Dog name: " + dog.name); // Accessing the protected variable } }
Output:
Animal makes sound
Dog name: Buddy
Explanation:
In this example, we have a superclass Animal with
- protected variable name
- protected method makeSound()
The subclass Dog can access the protected members of the superclass directly within the same class.
In the Main class, we create an object of the Dog class, set the name using the protected member name, call the protected method makeAnimalSound(), and print the value of the protected variable name using the object. The output shows that the protected members are accessible within the same class.
Example 2 of Protected Access Specifier in Java
Accessing Fields specified with Protected Access Specifier in Class within the same package.
Code:
class Animal { protected String name; protected void makeSound() { System.out.println("Animal makes sound"); } } class Dog { Animal animal = new Animal(); void setAnimalName(String newName) { animal.name = newName; // Accessing protected member in a class of the same package } void makeAnimalSound() { animal.makeSound(); // Accessing protected method in a class of the same package } } public class Main { public static void main(String[] args) { Dog dog = new Dog(); dog.setAnimalName("Buddy"); // Setting the name using protected member dog.makeAnimalSound(); // Accessing the protected method System.out.println("Animal name: " + dog.animal.name); // Accessing the protected variable } }
Output:
Animal makes sound
Animal name: Buddy
Explanation:
In this example, we have
- class Animal with protected members.
- Another class Dog in the same package
The Dog class can access the protected members of the Animal class directly, as protected members are accessible to classes in the same package.
In the Main class, we create an object of the Dog class, set the name using the protected member name, call the protected method makeAnimalSound(), and print the value of the protected variable name using the object. The output shows that the protected members are accessible in classes of the same package.
Conclusion
In conclusion, the protected access specifier in Java allows members to be accessed within the same class, subclasses, and classes in the same package. It provides a level of encapsulation and can be useful when we want to expose certain members to subclasses or classes in the same package but restrict access to classes in different packages.
Frequently Asked Questions(FAQs)
Here are some Frequently Asked Questions on Protected Access Specifier in Java.
Ques 1. Can the members declared with Protected Access Specifier in Java be accessed from outside the class and package?
Ans. No, protected members cannot be accessed from outside the package, except for subclasses that inherit from the class containing the protected members.
Ques 2. Can we override the protected methods in the subclass?
Ans. Yes, protected methods can be overridden in a subclass, just like public and default methods
Ques 3. How is the protected access specifier in Java different from the public access specifier?
Ans. The main difference between protected and public access specifiers is that protected members are accessible within the same class, subclasses, and classes in the same package, whereas public members are accessible from anywhere in the program, including outside the package.
Ques 4. Can protected members be accessed in an interface?
Ans. No, protected members cannot be accessed in an interface, as interfaces only support public and default access specifiers for members.