Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)

What is Protected in Java and How to Implement it?

Published on Sep 18,2019 3.6K Views

20 / 72 Blog from Java Core Concepts

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. in package edu1

package edu1;

public class EduProtected {
		 protected void message(String disp){
		     System.out.println("Package 1 message recieved: "+ disp);
} 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");


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.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 18th May,2024

18th May

SAT&SUN (Weekend Batch)
View Details

Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is Protected in Java and How to Implement it?