Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect information in the documentation on class hierarchies #2995

Open
keliss opened this issue Dec 10, 2023 · 0 comments · May be fixed by #2997
Open

Incorrect information in the documentation on class hierarchies #2995

keliss opened this issue Dec 10, 2023 · 0 comments · May be fixed by #2997

Comments

@keliss
Copy link

keliss commented Dec 10, 2023

Hello. I was going through the handbook, and noticed this part about Java:

Java, for example, considers this to be legal.

This statement isn't true. Of course, if you put all 3 classes in the same package it will look like it is, but it's not what the example in the handbook is about - class hierarchies, it's about same-package access.

package org.example;

public class Base {
    protected int x = 1;
}
---
package org.example.sub;

import org.example.Base;

public class Derived1 extends Base {
     protected int x = 5;
}
---
package org.example;

import org.example.sub.Derived1;

public class Derived2 extends Base {
    public void f1(Derived2 other) {
        other.x = 10;
    }
    public void f2(Derived1 other) {
        other.x = 10; // Error: 'x' has protected access in 'org.example.sub.Derived1'
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant