3

In java, there's three levels of access:

  • Public - Open to the world
  • Private - Open only to the class
  • Protected - Open only to the class and its subclasses (inheritance).

So why does the java compiler allow this to happen?

TestBlah.java:

public class TestBlah {

    public static void main(String[] args) {
        Blah a = new Blah("Blah");
        Bloo b = new Bloo("Bloo");
        System.out.println(a.getMessage());
        System.out.println(b.getMessage()); //Works
        System.out.println(a.testing);
        System.out.println(b.testing); //Works
    }
}

Blah.java:

public class Blah {
    protected String message;

    public Blah(String msg) {
        this.message = msg;
    }

    protected String getMessage(){
        return(this.message);
    }   
}

Bloo.java:

public class Bloo extends Blah {
    public Bloo(String testing) {
        super(testing);
    }
}
4
  • 1
    There is also package access. It is the default with no modifier. Commented Dec 2, 2008 at 3:19
  • are you sure that's right - a.testing and b.testing are not declared. did you mean a.message and b.message? Commented Dec 2, 2008 at 3:19
  • 1
    There are four levels of access. Works as designed. Commented Dec 2, 2008 at 4:50
  • I was hoping Java would have the access similar to C++ and C#. +1 for the question Commented Jan 10, 2010 at 0:47

5 Answers 5

14

Actually it should be:

Open only to the classes on the same package the class and its subclasses (inheritance)

That's why

Sign up to request clarification or add additional context in comments.

Comments

11

Because protected means subclass or other classes in the same package.

And there's actually a fourth "default" level of access, when the modifier is omitted, which provides access to other classes in the same package.

So protected is between default and public access.

Comments

5

To be more specific, you're expecting protected to work as it does in C++.

However, in Java, it has a different meaning. In Java, a protected method is available to the class (obviously), all the other classes in the same package and any subclasses of this class. Classes in other packages will not have access unless they subclass this original class.

See this similar question for more specific information on inheritance markers.

Personally, I almost never use protected. I develop applications rather than frameworks so I'm much more likely to define public methods, private data and, quite often, mark my whole class as final.

Comments

5

There are actually four levels of access: "public", "protected", "private" & default also known as package private or package protected. Default limits accessibility to the package. Default is quite useful and I use it frequently.

Comments

3

You're able to call b.getMessage() because b is of type Bloo, which extends Blah, and getMessage() is protected. Protected, as you mentioned, allows subclasses to access the method.

You've got the following errors, though:

  • Calling super() with no arguments in the Bloo constructor is an error. The compiler can't find the no-parameter Blah constructor because you defined one with a String parameter.
  • Calling new Blah() in TestBlah main method is an error for the same reason as above.
  • Referring to a.testing and b.testing is an error because you didn't define the variable testing for any class.

1 Comment

He heh I gues I'll never know if he did that on purpose. Hey that's funny the lizard answering to the dragon. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.