*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes probably statement of the obvious regarding class member access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "probably statement of the obvious regarding class member access" Watch "probably statement of the obvious regarding class member access" New topic
Author

probably statement of the obvious regarding class member access

mark juszczec
Ranch Hand

Joined: Aug 20, 2011
Posts: 35
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

Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
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?


OCPJP6-05-11
"Your life is in your hands, to make of it what you choose."
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
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

Joined: Aug 20, 2011
Posts: 35
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

Joined: Apr 13, 2011
Posts: 206

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

Joined: Aug 20, 2011
Posts: 35
Tommy Delson wrote:
but the first statement is not.



I'm sorry, which statement did I get incorrect?
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
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

Joined: Aug 20, 2011
Posts: 35
"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

Joined: Apr 13, 2011
Posts: 206
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

Joined: Aug 20, 2011
Posts: 35
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

Joined: Apr 13, 2011
Posts: 206
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: probably statement of the obvious regarding class member access
 
Similar Threads
Protected access modifier
Protected............
Kathy/Bert - great explanation of protected members
Protected members
javadoc help