Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the protected modifier mean?

I am reading the book The Java Programming Language, 3rd edition.

In chapter 3.5 , it illustrates the protected modifier with the following words:

More precisely, beyond being accessible within the class itself and to code within the same package, a protected member can also be accessed from a class through object references that are of at least the same type as the class that is, references of the class's type or one its subtypes.

The words makes me confused, in two aspects:

1. protected member can be accessed by code within the same package ? What I knew before is protected member can only be accessed by the subclass...

2. I don't understand what does a protected member can also be accessed from ... mean, anyone can explain to me please?

like image 591
Leem.fin Avatar asked Dec 26 '11 17:12

Leem.fin


People also ask

What does Protected mean in code?

In Java, protected means that the member can be accessed by any class in the same package and by subclasses even if they are in another packages. Note A protected variable is not visible outside the package.

How do you use a protected access modifier?

The protected access modifier is accessible within package and outside the package but through inheritance only. The protected access modifier can be applied on the data member, method and constructor. It can't be applied on the class. It provides more accessibility than the default modifer.

What does Protected mean in Java?

The protected keyword in Java refers to one of its access modifiers. The methods or data members declared as protected can be accessed from. Within the same class. Subclasses of the same packages. Different classes of the same packages.

What does the protected modifier accomplish?

protected access modifier in Java The protected access modifier in Java denotes that the variable or a method is accessible to its subclasses but private to all other classes outside the current package. It allows access to the class itself, subclasses and all classes in the same package to access the members.


2 Answers

  1. Yes, protected members can be accessed from the class itself, subclasses of the class and also all classes in the same package of the class (doesn't matter if those are subclasses or not). If you didn't know that last part before, then you've just learned something new.

  2. It simply means that you can use those members; if a member is not accessible, it means you'll get a compiler error when you try to use it.

like image 124
Jesper Avatar answered Oct 03 '22 05:10

Jesper


In Java, protected means that the member can be accessed by any class in the same package and by subclasses even if they are in another packages.

Note

A protected variable is not visible outside the package 

for example B extends A and A has a protected int x; it can be use within the class B. But cannot be access using its instance variable

like image 22
Hussein Zawawi Avatar answered Oct 03 '22 05:10

Hussein Zawawi