Java provides a set of keywords called Access Modifiers which helps us in setting the visibility of a class, interface, variable, data member, method, constructor, etc. There are 4 types of access modifiers, default, public, private and protected, which are supported in Java. In this article, I will be focusing solely on protected in Java and help you in getting clear insights into it.
Below are the topics I will be discussing in this article:
What is Protected in Java?
As mentioned before, protected in Java is an access modifier that helps a programmer in assigning the visibility of a class, its members, interfaces, etc. When class members are declared as protected in Java, they are accessible only within the same package as well as to the direct subclasses of the base class. While using protected keyword in Java, you must keep in mind that only class members can be declared as protected. Classes and interfaces can’t be declared as protected in Java.
Now you might be thinking why classes and interfaces can’t be made protected?
Well, if you think logically the answer becomes quite clear. If a class is made protected, then it will be only visible to the classes present within the same package. Now, as I have mentioned before, when anything is made protected in Java, it is visible to its subclasses as well.
But, here is an ambiguity. For other classes to extend a protected class, the parent class has to be visible. How will you extend something which is not visible in the first place? Thus, this causes ambiguity and creating protected classes is not allowed in Java.
Now let’s understand why interfaces can’t be made protected. Well, in Java the elements are generally made protected so that their implementations can be shared among others. But in the case of interfaces, they have no implementation, so there is no point in sharing them. Thus all the methods present within the interfaces must be public so that any class or struts can easily implement them.
Thus, you can declare only the methods and the data members as protected in Java and not the classes or interfaces. This helps in encapsulating the code by restricting the accessibility to the class members. It also helps in preventing the misuse of data.
Protected Implementation
Let’s now try to implement what we have learned till now. So, here I will be creating two classes, each belonging to a separate package.
EduProtected.java in package edu1
package edu1; public class EduProtected { protected void message(String disp){ System.out.println("Package 1 message recieved: "+ disp); } }
EduSubClass.java in package edu2
package edu2; import edu1.EduProtected; public class EduSubClass extends EduProtected { public static void main(String[] args) { EduProtected show = new EduProtected(); // invokes message() from EduProtected class show.message("Hello from package2 subclass"); } }
Output:
Package 1 message received: Hello from package2 subclass
This brings us to the end of this article. If you want to know more about Java you can refer to our other Java Blogs.
Now that you have understood what is protected in Java, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this article and we will get back to you as soon as possible.