aspose file tools*
The moose likes Beginning Java and the fly likes protected member access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "protected member access" Watch "protected member access" New topic
Author

protected member access

marks gorge
Greenhorn

Joined: Jul 05, 2009
Posts: 4
I am reading the SCP for Java 6 by S&B. Good book, like it.
Small question from the first chapter.
It talks about the protected/default variable and method declaration. Here is the piece of code:
----
package certification;
public class Parent{
protected int x=9;
}
--
package other;
import certification.Parent;
class Child extends Parent {//inheriting the parent

public void testIt () {
System.out.println("x is " + x); // call 1
}

public static void main (String[] arg) {
System.out.println("x is" + x); //call 2
}

}

======
My question. call to x from call 1 is fine, but it fails to compile due to call 2. My assumption is since it was declared protected, it sh be visible in the subclass thru inheritence all the time.? Am I doing somethign wrong.? It works if use the reference of the child class in the main method.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

This error has nothing to do with visibility. The member "x" is an instance field, and therefore needs an instance to be invoked. If you change call 2 to the following it will work:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


marks ,

Call 2 is nothing to do with any access specifiers (protected).. It should throw static error , as you can see that x is non-static and cannot be accessed inside static context. either x should be static , or it has to be accessed with object (because static does not have any object associated with it)

did you try the same void main method inside Parent class ?

Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

yeah Rob beaten me !!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Rob can be very quick.

And welcome to JavaRanch, Marks Gorge
marks gorge
Greenhorn

Joined: Jul 05, 2009
Posts: 4
Hi friends, thanks for the replies.
My reason of confusion is the examples stated there in the book. I will give another one.
Many a times while xplaining the scope of protected/default modifiers it explains via main methods. And that consfuses me. Here is another one.

package certification;
public class OtherClass {
void testIt() { //default member method
System.out.println("OtherClass");
}
}
---
package somethingElse;
import certification.OtherClass;
class AccessCass {
static public void main(String[] args) {
OtherClass o = new OtherClass();
o.testIt(); // here it sh fail becoz of the default modifier. And it does fail.
}
}
====
It does fail with error like modifer is not public etc....

this example explains about the default modifier. Now Why does it need to explain like this in the main method.
Hence my first question !
cchetan jain
Ranch Hand

Joined: Jul 05, 2009
Posts: 30
hi ..marks george..as we see that testit() is default member of the class and its accesibility is only in that package..when we use this class in other package then somethingelse class can see the other class but not its method testit()..that's why it is creating a problem.ok
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 375
Hi Marks,
The main method is part of the class even though it's a static method. Any class can have its own main method and they belong to the class they are defined in.

When you do o.testit(); in the main method, in effect the AccessCass is trying to access a default access member of the OtherClass.

Since OtherClass is in a different package access is not allowed.
marks gorge
Greenhorn

Joined: Jul 05, 2009
Posts: 4
Hi Gamini / cchetan , thanks for your inputs.

Here is my point. When I provided my first piece of code above in the discussion. I was using protected declaration.
And as per rules(of protected member thru inheritence) I should be able to get the //call 2 without any error in the main method. But it did not work as expected due to static / non-static scope issue.

Now When we are trying to access default declare member , we are trying to says that static/non-static rule does not apply and we need to think interms of protected and default declaration rule here?
I think there is something more to it which I am not able to understand.

Any help would be appreciated.

regards
marks
marks gorge
Greenhorn

Joined: Jul 05, 2009
Posts: 4
I think we need to think in 2 term here :

1> access modifiers(protected & default) in the superclass
2> calling from main or the member method of the subclass

for default member:
1> this is not available to other package by any means (not even thru reference of parent/subclass)

for protected member:
1> this is available to other package thru inheritance to the subclass member methods(directly)
2> this is available to main method of the subclass via subclass instance(reference notation) only

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
You are still making things too complicated. Access has nothing to do with main methods.

  • Private access: accessible from the same class only.
  • No modifier = default access, often called package-private. Access from any class in the same package.
  • Protected access: access from any class in the same package, AND access from subclasses of that same class.
  • Public access: access from any Java code which is in the classpath.

  • Remember the keywords are public private protected, not Public Private Protected. Some people think private access means access from the same object, but that is mistaken. It allows access from other objects, of the same class only.
    santosh kimothi
    Ranch Hand

    Joined: Jun 10, 2009
    Posts: 32
    x is the non static class variable, and main is the static method, so inside static main method you can't access non static method with just using that variable name, you have to first allocate the memory for x,

    but in first method it is allowed because that is non static method, and inside that method x will only be executed only when object will be created,

    thats the reason in main method you have to either create first object of that class or declare x in that class as static.


    Santosh Kimothi,
    Java programmer
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: protected member access