This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

probably statement of the obvious regarding class member access  RSS feed

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks

I think I'm having another epiphany.

The class member access modifier rules - whether you can see a class member or not from inside the class/inside a subclass/in same package/in diff package - impact not just ability to access a var or method but also

ability to override
ability to overload
ability to subclass
visibility from inner classes

and probably more stuff I can't think of.

Also, this is also probably obvious, but when access modifiers are applied to classes, they perform similar restrictions in visibility.

For example, a public class is visible everywhere
a class with default access is only visible to other classes within the same package.

Am I right?

Mark

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about the protect member or class? What's is the restriction or can a subclass in different package able to access it superclass's protected members?

What about a default access modifier?

 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mark juszczec wrote:For example, a public class is visible everywhere
a class with default access is only visible to other classes within the same package.

Am I right?



You got it!
 
mark juszczec
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tommy Delson wrote:What about the protect member or class? What's is the restriction or can a subclass in different package able to access it superclass's protected members?

What about a default access modifier?



Classes can't be protected. Well ok, I peeked at my notes and only inner classes can be protected.

If I'm right with this little epiphany, then anything marked as protected is only accessible from within its class, from anything in its same package and only by inheritance from other packages.

Anything marked with the default access modifier is visible in its class and anything in its same package and that's all.
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Good...You got last two statements correct, but the first statement is not. Try to confirm it in coding, and let me know what do you think.

Recreate the scenario your stated earlier in coding, this will confirm and clear your doubt.

Try coding and apply the concept in different scenarios or in many ways, this will help you on the exam and reinforce your knowledge.

A little warm up on the topic...

 
mark juszczec
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tommy Delson wrote:
but the first statement is not.



I'm sorry, which statement did I get incorrect?
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mark juszczec wrote:

Tommy Delson wrote:
but the first statement is not.



I'm sorry, which statement did I get incorrect?



Here you go:

"Classes can't be protected. Well ok, I peeked at my notes and only inner classes can be protected. "

 
mark juszczec
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"Classes can't be protected. Well ok, I peeked at my notes and only inner classes can be protected. "





compiling this results in:

c:\Users\markj\Desktop>javac ProtectedClass.java
javac ProtectedClass.java
ProtectedClass.java:1: modifier protected not allowed here
protected class ProtectedClass{
^
1 error

However this:



compiles just fine.

I'm confused as to what part of the statement was wrong?

Mark
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right, my bad I mixed up. In fact a class can not be declared with protected & private because of the visibility from other class to access. For class member you can declare all 4 access modifiers.

If the class declared protected only a subclass able to access it so, it doesn't make sense to have a protected class since only subclass able to access.

(Personally I think they should allow to have a class declared "protected" for security purpose, but thinking in a big picture it's not flexible and reusable to have have a class declared protected. I think there is a good reason they not allow it...)

If the the class declared private, no class able to access it except inner class within the private class so, it doesn't make sense to have a class declared private.

After review from the K & B book on chapter 1 and here is the recap that I got :

"a class can be declared with only public or default access;"
 
mark juszczec
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tommy Delson wrote:You're right, my bad



nah, its all good. i thought the whole point was to get me to write some test code. Which I did, just to double check.
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, that also my point as well to have you confirm by coding, that's how to prepare for the OCPJP put theories and concepts in action.

So many things & rules that need to remember...only through coding to confirm & clear the doubt.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!