-3

Good day,

if I have a class somewhat like this:

public class A {
        protected class B {
                public int element;
                public int get() {
                        return i;
        }
}
public class C {
        foo();
}
public class D extends A {
        foo();
}

1.Could foo() in class C, access element and get()? Or can only class D access them?

  1. If class B was private, could C access element and get()?
Matthias
  • 23
  • 1
  • 4
  • First, the code doesn't compile. So it's impossible to answer. – Tunaki Feb 14 '16 at 14:27
  • If ever the code was compiling, why wouldn't you try it and find for yourself? – arainone Feb 14 '16 at 15:40
  • No, `C` can not access `element` or `get`. `D` can though. If `B` was private no class except `B` could access the members. Here's a good table for future reference: http://stackoverflow.com/a/33627846/276052 – aioobe Mar 08 '16 at 21:00

1 Answers1

0

Only D, class B is protected, then C can't access to class B.

But, you have to instantiate B in D to call to get() and get i, because get is an instance method.

If B was private, neither C neither D can access B.