• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

protected member access

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah Rob beaten me !!
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rob can be very quick.

And welcome to JavaRanch, Marks Gorge
 
marks gorge
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Ranch Hand
    Posts: 32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic