This week's book giveaway is in the Java in General forum. We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line! See this thread for details.
Well your Subclass extends Superclass so it inherits the variable f. remember protected members have package level access and they are inherited by classes outside the package . So other classes which are not the subclasses cannot access it. here in this situation Subclass of package p2 is accessing f( belonging to Superclass and of package p1) because it inherits it as f is marked protected. Now Anotherclass is not a subclass of either Subclass or Superclass. So f is inaccessible by Anotherclass.
Remember that since the class Superclass is marked public , you are allowed to access the class from any package( so you instantiated it ) but protected members have package level and are imherited by subclasses only.
refer to chapter 1 , page 36 of Kathy Sierra & Bert Bates.
((While I writted this it is possible that I got many more replays than few, maybe even six, or four, on which I am replaying))
Yes, yes, yes... Very much thank you on such a serious answers, and good. And fast. There is just this one moment: "So you instantiated it". I can use inherited protected member in a subclass, through inheritance as own subclas'es variable, but I didn't do that. It's what happened after instantiation. I used a protected member.
A) STAYS IN A PACKAGE AND SUBCLASS.
I know that I shouldn't be able to get protected member outside of both, of the subclass and outside of the package where it is declared protected.It should work through inheritance outside the package, (But it works through instantiation as well, in one case).
B) INSTANTIATING SUBCLASS.
I know that I can not instantiate (instantiate and use) Subclass and use protected member outside of the package of the Superclass. ( Except if I instantiate a Subclass in a Subclass'es method body - that's a question about, and it's happening outside of the superclass'es package, "the protected area")
C) SUBCLASSING ALL AROUND.
I know that by subclassing i can "drag" around the protected variable through the packages. (I think so), as long as I am doing it with the public classes around, and inheriting, but I can't do the same thing with instantiating here and there (except in a package from where it comes from, becaouse it is protected, AND in the subclass'es method body, wherever that package is)
1) I initialized a protected varible, and used it. Ouside*. ( but in a subclass's method body)
2) I can't do the same in any other class outside*.
*outside of the superclass'es package where the questioned protected member was declared as protected.
I can not connect statement 1 and statement 2 , here above, except through something like:
"In a subclass I can do whatever I want becaouse of the PROTECTED modifier somewhere else, which guarantiees me that I can use the PROTECTED variable in a subclass ALLWAYS, even through instantiation, not only through inheritance" ( and nevermind now the packages becaouse inheritance and keeping a variable safe is more important. )
So , my question is like:
Is this really so, as what I described?
(When I compile it, it behaves in this way which I described, but I am not used on talking about programming)
If it is understandable what I described, is this something that you know for?
Is this something that is common?
Hi Petar, when you inherit a protected member in a class , it then behaves like private to that class. You are allowed to access it through instantiation (in a static method of the class)and also in any non static method without instantiation. But you are not allowed to access it outside the class through instantiation.Consider the following code:
It compiles without errors.
So when a variable is private to a class it can be accessed inside any code in the class and if you are using in a static method inside the class, then you have to instantiate the class and then access it .but since the static main() method is inside the class it can be accessed through instantiation.
Same is the case with protected members. Inside the subclass which inherited it , you can access it anywhere (static and non-static context both).