0

I'm new to java and getting the hang of protected visibility.

I have Class A which has protected method method1 as follows -

public abstract class A { 
    public A(...){}

    protected void method1(){
        // do something
    }

Now I have another Class B which extends a different Class C. Class B has a nested static class, Class D extending Class A

public class B extends C {
    public B(...){
        super(...);
    }

    private static class D extends A {
        D(...){super(...);}
    }

    public void method2() {
        D record = new D();
        record.method1(); // need to access the protected method1 of class A, which is the superclass of nested class D. Getting the error here
    }
}

I get this error - method1 has protected access in Class A Now my question is how can I access method1? I cannot change it to public in class A

4
  • method1 is not static, so you cannot use D.method1();, you need to use record.method1(). I guess this is what you wanted to do anyways because record is not used otherwise. Commented Jun 13, 2022 at 8:52
  • record.method1(). The method isn't static, so you call it on an instance of A Commented Jun 13, 2022 at 8:53
  • My bad that was a typing error, I am indeed accessing it using record.method1(). But still unable to access it. Commented Jun 13, 2022 at 8:55
  • One warning: sind java 15 there exists record - an immutable class for tuples, like record Complex(double re, double im) {}; Complex z = new Complex(3.0, 4.0); double r = Math.hypot(z.re(), z.im()); Commented Jun 13, 2022 at 9:14

3 Answers 3

3

You need to create a proxy method in class D as outlined below. Class D can access Class A's protected methods, but an instance of Class D can't call protected methods directly, and must have a proxy method to call the protected method of the super class.

In class D, you need to create a public getter for the method in Class A, so:

public class A {
    protected void protectedMethod() {}
}

public class D extends A {
    public void callProtectedMethod() {
        super.protectedMethod();
    }
}

final D record = new D();

record.callProtectedMethod();
Sign up to request clarification or add additional context in comments.

Comments

0

Since A.method1() is protected, it is only accessible from same class/same package/child of A.

In your code, your are calling A.method1() in B, that is not accessible if B not in same package as A

The solution is using a wrapper as @Alex Ander or change modifier of A.method1() to public

Java Access Modifiers

2 Comments

Thank you for the explanation, B is indeed not in the same package as A. using the wrapper worked.
Yeah, of course it work. But I would recommend you learn the mean of modifiers so that you can design java code more reasonable
0

The problem was caused by the protected method1() called from method2(), which belongs to B, which is outside A and D, and B is not in the same package with A or D.

The method1() should only used/called inside A or D. That's what protected is used for, to ensure it is used only in the owner class, its inheriting classes or from the same package.

That's why when the wrapper method created inside D, the problem solved, since the protected method1() is called from inside D.

We still can override the protected method1() of A and make it public through D if necessary. (Modified from the wrapper example above)

class A {
    protected void protectedMethod() {}
}

class D extends A {
    @Override
    public void protectedMethod() {
        super.protectedMethod();
    }
}

final D record = new D();
record.protectedMethod();

Comments

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.