Default/protected access question

Why does this work:
The class below extends TreePath but is in a foreign package to TreePath. The call at *1* is to a constructor in TreePath that has "default" access. Shouldn't this fail to compile since the "default" modifier only allows classes in the same package access? The call at *2* works as I would expect since the TreePath constructor being called there has "protected" access.
Thanks,
      * Subclass of treepath with needed public constructors.    
     private class ExtendedTreePath extends TreePath {
          public ExtendedTreePath(Object singlePath) {
               super(singlePath);                                        // *1*
          public ExtendedTreePath(TreePath parent, Object lastElement) {
               super(parent, lastElement);                               // *2*
     }

Hmm... That is odd. I get a slightly different, but no more helpful, compiler message. I know that an object has to implement Cloneable in order for the clone to succeed, but that's just a tag interface that should lead to a runtime exception.
I think a slight adjustment in thinking about "protected" explains it:
"Protected means it can be called by classes in the same package, and by subclasses, and this class (like every class) is a subclass of object, so why can't it be called?"
That's what I thought. What I would suspect is the case, though, has to do with clarifying the "and by subclasses" piece of that rather imprecises wording of the rule.public class MyClass extends Object {
    public void meth() {
        Object obj1 = new Object();
        Object obj2 = obj2.clone(); // or, I imagine, any protected method would fail here
        MyClass mine = new MyClass();
        mine.meth2(); // this should work, It think
        super.clone(); // this should work
    protected void meth2() {
} I don't really know how to word this very well, but it seems that we can only call protected methods on super, or, I would guess, on other members of our class. We can call Object's clone (or other protected) method if we're call it on super, but not on an arbtirary object.
I don't know if that's correct, but play around with it, or re-read the relevant parts of the JLS and see if it jives.

Similar Messages

  • Protected access and Default access

    hi all,
    Can you tell exact difference between protected access and default access.
    Thanks in advance.

    default (for classes*), also known as "package" or "package private": accessible from within that class and other classes in the same package.
    protected: accessible from within that class, other classes in the same package, and subclasses.
    *Note that for interfaces, the "default" access is pubic, not "package private".                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Why does protected access also allow access from classes in same package

    Having been asked the question and found myself unable to answer it, I wonder if anyone can explain the reasoning why protected access implies the default access as well?

    Sure:
    ---------- class A ------
    package pkg;
    public class A
    protected static int _a;
    private int _i;
    public A() { _i = _a++; }
    public String toString() { return "instance "+_i; }
    --------- class B ------
    package pkg;
    public class B
    public B() { A._a = 27; }
    --------- class C -----
    public class C extends pkg.A
    -------- class main ----
    public class main
    public static void main(String[] args)
    pkg.B b1 = new pkg.B();
    pkg.A a1 = new pkg.A();
    pkg.B b2 = new pkg.B();
    C c1 = new C();
    System.out.println("a1 is "+a1);
    System.out.println("c1 is "+c1);
    The above classes are legal and compile and when
    run, the output is:
    a1 is instance 27
    c1 is instance 27
    (Please note, this is just an example of what is allowed and why I'm curious as to the advantages of this permitted behaviour, NOT what I would ever write!)

  • Access is denied. Verify that either the Default Content Access Account has access to this repository, or add a crawl rule to crawl this repository. If the repository being crawled is a SharePoint repository, verify that the account you are using has "Ful

    I am trying to resolve this after setting up my new Farm.I am having 2 wfe ,1 sppserver,1 server dedicated for crawl ,1 for search and index  in my farm. I guess dedicated crawl server  is the root cause of the issue,i also did
    disableloopback check settings but still facing the same issue,any solution?
    Please Mark it as answer if this reply helps you in resolving the issue,It will help other users facing similar problem

    Hi Aditya,
    Please refer to the links below and try if they help:
    Add the full read rights to Default Content Access Account of Search Administration via the web application’s user policy.
    http://sharepoint.stackexchange.com/questions/88696/access-is-denied-verify-that-either-the-default-content-access-account-has-acce
    Grant the Default Content Access Account permission in User Profile Service Application
    http://www.sysadminsblog.com/microsoft/sharepoint-search-service-access-is-denied/
    Modify you crawl rule
    http://wingleungchan.blogspot.com/2011/11/access-is-denied-when-crawling-despite.html
    Add crawl servers ip to local host file
    http://wellytonian.com/2012/04/sharepoint-search-crawl-errors-and-fixing-them/
    Regards,
    Rebecca Tu
    TechNet Community Support

  • Private or Protected access for super class variables

    What is the best practice...
    Assume there is a class hierachy like
    Person (Super class) / StaffMember/ Professor (Sub class)
    1) The best way is to keep all the instance variables of each and every class private and access the private variables of super classes through subclass constructors (calling "super()")
    Ex:-
    public class Person {
    private String empNo;
    public Person (String empNo) {
    this.empNo = empNo;
    public class Professor extends Person {
    private String ........;
    private int ...........;
    public Professor (String pEmpNo) {
    super(pEmpNo);
    OR
    2)Changing the access level of the super class variables into "protected" or "default" and access them directly within the sub classes...
    Ex:-
    public class Person {
    protected String empNo;
    public Person () {
    public class Professor extends Person {
    String ........;
    int ...........;
    public Professor (String empNo) {
    this.empNo = empNo;
    Thank you...

    i'd think that you'd be better off relaying your initial values through the super class's constructor that way you'll get cleaner code, there's a possibly of inconsistency with option 2. i.e. you can then write code in your super classes to generally handle and properly initialize the instance variables while in the case of option 2, you'll have arbitrary constructors performing arbitrary initialization procedures

  • Can I password protect access to Time Machine??

    Hey guys ..
    Ive just switched over from PC to mac. So far Im loving it and doubt Ill ever use a PC again.
    I do have a question about Time Machine though. Is there a way to password protect access to the Time Machine interface ?
    Cheers
    Darren

    Can't help with the TM stuff, since I don't use it, and will leave that discussion to macwiz1220; however, since you're new to Macs, once you sort out your issue, see these:
    Switching from Windows to Mac OS X,
    Basic Tutorials on using a Mac,
    Mac 101: Mac Essentials,
    Anatomy of a Mac,
    MacFixIt Tutorials,
    MacTips, and
    Switching to the Mac: The Missing Manual, Leopard Edition.
    Additionally, *Texas Mac Man* recommends:
    Quick Assist.
    Welcome to the Switch To A Mac Guides,
    Take Control E-books, and
    A guide for switching to a Mac.

  • Regarding Protected access specifier

    Hi ,
    I thought that indeed you could access protected data member from another package IF you are using Inheritance. It makes the data memeber in question "friendly" to the derived class. At least that is my understanding.
    You can not access protected data element directly in different packages. package test1;
    public class Test123 {
    protected int x=42;
    package test2;
    import test1.Test123;
    public class Test234 extends test1.Test123{
         public static void main(String args[]){
              Test123 ob=new Test123();
              System.out.println("x="+ob.x);
    but My question is that ..if u run this program its not compile..Compile time exception is throws saying that the field Test123.x is not visible..but according to protected access specifier this will work...and one thing that if i make field ' x ' of Test123 as public it is working... How it is possible ...can anybody pls help it out.. Thanx in advance...

    This access is provided even to subclasses that reside in a different package from
    the class that owns the protected feature.As you have found this (and similar) can be confusing.
    Have a look at the Java Language Specification (http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.7) "6.6.7 Example: protected Fields, Methods, and Constructors". Can you see the difference between the delta() and delta3d() methods?
    "A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object." As another example of this, the following compiles OKpackage test2;
    import test1.Test123;
    public class Test234 extends test1.Test123 {
        public static void main(String args[]) {
            Test234 ob = new Test234();
            System.out.println("x=" + ob.x);
    }When run it prints "42" - showing that the same d@mn field is being accessed! Like you, I'd appreciate hearing a simple rationale for this (if there is one).

  • Private, protected Access Modifiers with a class

    Why cant we use private and protected access modifiers with a class?
    Thanks.

    Matiz wrote:
    >
    Public access allows you to extend a parent class in some other package. If you only want users to extend your class rather than instantiate it directly, make the class abstract and design for extension.Agreed. However, would the same argument be not true for the default access at the class level? No. Default access would only allow you to extend a parent class in the same package (as opposed to some other package).
    Now my confusion is why is a class allowed default access at the top level and not protected?Because protected for a top-level class makes no sense. The protected keyword provides member access to any other class in the same package and extending classes outside the package. A top-level class isn't a member of a class, by definition, so there's nothing that protected would do provide differently than public.
    So, the two access modifiers for a top-level class are public and default. Public allows access to the class outside the package, whereas default restricts access to the class within the package.
    ~

  • Protection levels question

    Is there a way to set the protection level of a class / method so that only classes in the classes package or super package has access.
    What I mean is that I have two packages: msglib & msglib.gui. I would like to create methods in msglib.gui that would only be accessable to classes in the two above packages.
    Is there a way to do this or should I just put all the classes in the same package and make everything the default protection level?

    Is there a way to set the protection level of a class
    / method so that only classes in the classes package
    or super package has access.No.
    Is there a way to do this or should I just put all
    the classes in the same package and make everything
    the default protection level?Depends upon the class relationships. Over use of mutual dependence between classes in the same package is a "Code smell", so consider whether your class design makes innate sense.
    See "Inapropriate Intimacy":
    http://wiki.java.net/bin/view/People/SmellsToRefactorings

  • I'm confused about protected access - any insight ?

    I'm trying to understand how the protected access modifier works. I have tried to compile some example code and I'm seeing behavior which appears to go against what the text I'm using claims should happen. To wit:
    My understanding is that any subclass and any class in the same package has access to the protected member of a class. Default access (when no access modifier is used at all) allows access only to classes in the same package.
    I have the following two classes:
    //--------  Parent.java ---------------
    package testpkg.p1;
    public class Parent
        public int x = 7;
        protected int getXValue ()
            return x;
    //--------- Child.java --------------
    package testpkg.p2;
    import testpkg.p1.Parent;
    public class Child
        extends Parent
        public static void main (String[] args)
            Child child = new Child();
            System.out.println("Child X: " + child.getXValue() + "\n");
            System.out.println("Parent X: " + child.testParent());
        public int testParent ()
            Parent parent = new Parent();
            return parent.getXValue();
    }I get the following compilation error:
    $ javac -d ../lib Parent.java Child.java
    Child.java:18: getXValue() has protected access in testpkg.p1.Parent
    return parent.getXValue();
    ^
    1 error
    Now it seems to me that everything should compile fine since the Child class is a subclass of the Parent class, and as such should have access to the protected member method getXValue(). However this obviously isn't the case. It seems that the behavior is more like what is described for default access, i.e. no access by any class outside of the package, no matter if it is a subclass or not.
    Can anyone enlighten me on this ? Am I missing something obvious ?
    Thanks in advance for any feedback.
    -James

    You know that you don't have to use the "protected"
    modifier.Yes you do.
    In fact, when I learned Java, it was called
    "package" scope, not protected. But the same
    principle applies.No, the principles outlined above and in the Java Language Spec apply.
    "protected" or "package" means that only classes
    within the package itself can have access. You do not
    actually have to place any modifier at all since this
    is the default scope.No, protected adds the ability to access ex-package super class members from a child class instance. Try compiling the above example without specifying protected access.
    Pete

  • Clone() has protected access

    I want to make a copy of a class instance. The class is Process. I've tried
    Process outpt = (Process)p.clone();
    I get clone() has protected access in java.lang.Object.
    How do I get a copy of a variable? Not a reference to the original variable?

    You can't, unless it has had a proper clone() method written for it. And Process hasn't, most likely because copying a Process doesn't make much sense. Is there a reason you need to copy it and not just make another reference to it? How would you expect the two choices to behave differently?
    (And your question should have asked how to get a copy of an object, not a variable. There's a difference between the two things.)

  • Default bind access on class fields?

    Hi,
    I have a question on binding. Look at the following code:
    1 var readyLight = Circle {
    2     centerX: 12
    3     centerY: 12
    4     radius: 12
    5     stroke: Color.GRAY
    6     fill: bind RadialGradient {
    7          centerX: 8
    8          centerY: 8
    9          radius: 12
    10          proportional: false
    11          stops: [
    12               Stop {
    13                    offset: 0
    14                    color: Color.WHITE
    15               }
    16               Stop {
    17                    offset: 1
    18                    color: if (readyButton.selected) Color.GOLD else Color.GRAY
    19               }
    20          ]
    21     }
    22}This circle uses a radial gradient to fill itself. The 18th line uses a condition statement to dynamically determine the color of the second stop of the gradient. However, I think this line should use a "bind" keyword to keep the color dynamic. But when I added a bind the 18th line, an error showed up:"color has script only (default) bind access in javafx.scene.paint.Stop". I am confused about this, I haven't seen any document saying the "default bind access" thing.
    This demo is from the official online UI tutorial: http://java.sun.com/javafx/1/tutorials/ui/layout/index.html
    Any help on this is appreciated!

    This works because you are creating a new RadialGradient instance whenever selected changes. You are not just changing the color.
    It would be more efficient to define a selectedGradient and an unselectedGradient and use them:
    def selectedGradient  : RadialGradient = RadialGradient {
              centerX: 8
              centerY: 8
              radius: 12
              proportional: false
              stops: [
                   Stop {
                        offset: 0
                        color: Color.WHITE
                   Stop {
                        offset: 1
                        color: Color.GOLD
    def unselectedGradient  : RadialGradient = RadialGradient {
              centerX: 8
              centerY: 8
              radius: 12
              proportional: false
              stops: [
                   Stop {
                        offset: 0
                        color: Color.WHITE
                   Stop {
                        offset: 1
                        color: Color.GRAY
    def readyLight = Circle {
         centerX: 50
         centerY: 50
         radius: 12
         stroke: Color.GRAY
         fill: bind if (selected) selectedGradient else unselectedGradient
    }

  • Undeletable protected access points HELP!

    Hi,the problem is when I open destinations in my N8 there are 106 access points there all called 'boingo',and i can't delete any of them.When I try to delete it says unable to delete protected access point.I've tried updating software and also making 'factory settings' but nothing changes.That *hits are always there !!!
    Solved!
    Go to Solution.

    Thanks mate., even it has been 4 years since I posted this question. I came back here because Microsoft sent me an e-mail regarding the merging of Nokia Discussions account to my Microsoft Account. Good old days. RIP Nokia.

  • How do you use Default Resource Access Information?

    I have some 10g Forms & Reports that I want to use with SSO and they will all be connecting to the database with the same connection info. I know how to configure a Default Resource Access Information, but how do you use this with Forms & Reports?

    Douglas,
    the default Resource Access Infomation should be the connection information right? This is used in conjunction with SSO. You need to configure your F&R applications to delegate authentication to SSO by placing ssoMode=true in the config section of formsweb.cfg.
    The Forms Servlet will connect to OID retrieve the Resource Access Information (descriptor) for a given user and automatically log them into the application.
    Users will need a global identity in OID and SSO must be enabled to use resource access info with F&R
    regards,
    tt

  • ACS 5.3 cannot create default network access authorization rule

    Hi, when I click 'Create...' under Access Policies > Default Network Access > Authorization, and then press the 'OK' button, it says 'Please configure at least 1 condition.' However I have no way to configure conditions as the 'Conditions' text is just bold text and not a link or any sort of configurable area. If I go to 'Customize' on the bottom right and add conditions to the right list box, I still have no options when I press Create. Also, the 'green light' next to Default Network Access is grey with a line through it. This is the most cryptic system I have ever used.. anyone have an idea? Thank you!

    Looks like you are using chrome amd it's not a supported browser.
    Supported Web Client/Browsers
    You can access ACS 5.3 administrative user interface using the following Web Client/Browsers:
    •Windows 7 32 bit
    •Windows XP Professional (Service Pack 2 and 3)
    •Windows Vista
    •Internet Explorer version 7.x
    •Internet Explorer version 8.x
    •Internet Explorer version 9.x
    •Mozilla Firefox version 3.x
    •Mozilla Firefox version 4.x
    http://www.cisco.com/en/US/docs/net_mgmt/cisco_secure_access_control_system/5.3/release/notes/acs_53_rn.html#wp222016
    Jatin Katyal
    - Do rate helpful posts -

Maybe you are looking for

  • DB does not look the same in different screen resolutions

    Hi All - I have built my dashboards using the Windows screen resolution 1024 x 768, and exported the dashboard into powerpoint.  With this resolution the dashboard looks normal.  I then changed my Windows screen resolution to 1280 x 1024, opened the

  • How can I delete an old file from my library on iTunes

    Occasionally I have to replace PDF manuals with ones that have been updated.  But after I sync the new file the old one remains on my Mac itunes library.  How can I delete the old file?  So far all I do is deselect the old file but it's still there. 

  • Can I switch purchases from one account to another?

    I have an old iTunes account that I bought a movie on and I would like to switch it to my new account. Is that possible?

  • Installation with Raid, etc... [dead issue, don't bother reading]

    I'm trying to install arch using RAID and LVM, following the wiki procedure in Installing with Software Raid or LVM. I am able to get to the point where I install the kernel.  When I install it, the bottom of the screen flashes the "wrong fs type, et

  • Sync iphone and MobileMe

    new contact and calendar information on my iphone will not sync up to MobileMe or directly to the computer even if they are connected. How do I sync them? Push is on and so is automatic syncing.