This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

protected member subclassing

 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is more like checking am I right in what i see.

Given:

and

and...


Question is... When I compile AnotherClass i get the error:


Why I don't get the same error when I compile the class Subclass?


 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because protected members are visible to clases within the same package and to subclasses ...

As opposed to default members, which are only visible within the same package.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To access a protected member of one class say c1 in a package say p1, from another class say c2 in another package (p2) you must make c2 inherit c1 (or another class in p2 that inherits c1).

Please correct me if I am wrong.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well what Saima says is right but lets say it simple way:

protected members can be accessed only bye the classes in the same package and the subclasses.

that means any class in different package cannot access protected members unless it is a subclass not even the other classes in the same package of the subclass.
 
Petar Thomas
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
((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?




In a subclass I instantiated
a protected member
 
Petar Thomas
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And thank You mister Sebastian Janisch, thank You mister Venu Chakravorty, and thank You Saima Kanwal, and thank You Neha Daga.


( It's not only through inheritance , it stll seams to me so. And thank you Neha Daga one more time for other post replay)
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

Hope this helps you.
 
Petar Thomas
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it does. My horses are excited. They love evrything, and evryone, when they see the distance near. I heard what I needed to hear. Cheere-a-yo, let the silver and gold alone. Horses. Let's goooo
 
Does this tiny ad smell okay to you?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic