File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected member access

 
marks gorge
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20368
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 874
Android Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah Rob beaten me !!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob can be very quick.

And welcome to JavaRanch, Marks Gorge
 
marks gorge
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 378
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 32
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic