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".                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Similar Messages

  • 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.

  • I want to uninstall and then reinstall Firefox but I can't uninstall it. Nothing happens when I click remove in program access and defaults and the helper.exe file in the firefox uninstall dir doesn't do anything either.

    I want to uninstall and then reinstall Firefox but I can't uninstall it. Nothing happens when I click remove in program access and defaults and the helper.exe file in the firefox uninstall dir doesn't do anything either.

    "program access and defaults" is not the place to remove programs in Windows. You need to go to the control panel and click on Add and Remove programs. For instructions on how to uninstall Firefox, see [[Uninstalling Firefox]]

  • 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!)

  • 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.
    ~

  • 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

  • Problems with protected access in java.util.vector

    Hi,
    basically i'm trying to use the method removeRange(int,int) of class Vector from the java API.
    I keep getting the following compiler message:
    removeRange(int,int) has protected access in java.util.vector.
    This seems rather vague, and i don't know quite what to do?
    Thanks in advance

    removeRange(int,int) has protected access in java.util.vector.
    This seems rather vague, and i don't know quite what to do?As error messages go, that's pretty clear: method removeRange is a protected method and you can only call public ones of Vector, right? You can always combine subList and clear:
    vec.subList(fromIndex, toIndex).claer();

  • 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).

  • Remember username when connecting to password protected Access DB

    We have an applet which connects to a password protected Access DB using JDBC. In IE, NS, and VAJ VM's we can connect to the database with the password without giving a username.
    Now I have tried running the applet against the Java Plugin 1.4 VM and the Microsoft driver reports that the password is incorrect.
    However, I figured out that if I connect with "Admin" as user name things work out fine.
    Thought someone would like to know.
    Should this be reported to Sun, or is this sufficient?

    Thanks, it was helpful

  • Protected access

    i read this and this and got the impression that i could do what i intent:
    i have a class that is initialized in the GUI class, so has access to the other's controls.
    when i try this:
    String a = visual.jPasswordField.paramString(); // line 106i get an error msg stating:
    paramString() has protected access in javax.swing.JPasswordField at line 106
    So: cant i use that method? Why?
    both classes are in the very same package
    thanks in advance

    yes, and at a certain point i need to use a String -For the password? Read the [url http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JPasswordField.html#paramString()]docs for the method. It does not return the password, it returns a implementation-dependent string for debugging.
    Look up [url http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JPasswordField.html#getPassword()]getPassword(), and [url http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#String(char[])]String.String( char[] ).
    Returns a string representation of this JPasswordField. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.

  • Constructor LOVAttributeInfo has protected access

    I'm trying to build a dynamic combo box.
    So I built a view object and made the view link for it, etc. I then created an LOV rowset based off of it. Which is generating the following errors.
    "constructor lOVAttributeInfo(java.lang.String) has protected access in class oracle.dacf.dataset.LOVAttributeInfo"
    How do I fix this? It seems like this should be a simple fix, like checking some radio button in the view object, but I haven't found anything yet.
    null

    Try this instead:
    Date date=new Date();
    date.setTime(msec);
    dateOfPick.setTime(date);
    V.V.

  • 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.)

  • 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.

  • Protected access error when firing event

    My program is supposed to "press" a button when the user presses a certain key. I didn't use mnemonics because I didn't want the Alt key to be pressed, but just a key (i.e. Q, without any modifiers). So I decided to use fireActionPerformed to fire the event manually.
    However, when I compile, the compiler gives me an error saying: "fireActionPerformed(java.awt.event.ActionEvent) has protected access in javax.swing.AbstractButton.
    Why is this? How can I fix or get around this? Maybe there's a better way for me to accomplish my task?
    Any help greatly appreciated!

    Demo:
    import java.awt.event.*;
    import javax.swing.*;
    class ExampleAction extends AbstractAction {
        public ExampleAction() {
            super("ExampleAction");
        public void actionPerformed(ActionEvent evt) {
            System.out.println("ExampleAction.actionPerformed");
    public class KeyboardMappingExample extends JPanel {
        public KeyboardMappingExample() {
            KeyStroke ks = KeyStroke.getKeyStroke("ctrl Q");
            Action action = new ExampleAction();
            Object name = action.getValue(Action.NAME);
            InputMap im = this.getInputMap(WHEN_IN_FOCUSED_WINDOW);
            ActionMap am = this.getActionMap();
            im.put(ks, name);
            am.put(name, action);
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    JFrame f = new JFrame("ExampleAction");
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    f.getContentPane().add(new KeyboardMappingExample());
                    f.setSize(400,400);
                    f.setLocationRelativeTo(null);
                    f.setVisible(true);
    }

Maybe you are looking for

  • My ipod touch 5g was working 5 minutes ago now I can't turn it on!

    I have no idea if its the actual ipod itself or just the screen but when I press the button at the top, literally nothing happens and it was working about 5 minutes ago! My home button has been broken for a while so I cannot do a reset otherwise I wo

  • Trade-In Rebates for new iPods???

    Im a bit angry the new iPods are out so soon and I just got mine not even a year ago. The big thing for me is the battery power - _(Even the new Nano) 3X 's what Ive got in the 30gb._ Does anyone know if Apple will be offering any trade in rebates of

  • CS6 Error 16 Code after installing on new computer.

    I just purchased a new Mac Pro Desktop. My old machine used Snow Lepoard and this is Mountain Lion.  After migrating data from my ole machine to this one, I am unable to open any program in CS6. When I attempt to, I receive a Error 16 advising me to

  • Connecting J2ME serversocket with J2SE socketconnection

    ***********connecting J2ME serversocket with J2SE socketconnection ***************** I am currently working on J2ME . Here I have a situation that I need to contact the push registry's socket connection of a midlet from the web server . I apparently

  • Friends "buddy list" not showing after the last update..!!

    I just downloaded the update and for some reason i cant see my friends online, i tried restarting couple of time and its still the same, and two of my other friends are having the same problems, any advise ?