SE450: Scope: Visibility in Java [11/47] Previous pageContentsNext page

What is public visibilty?

What is private visibility?

What is package-private visibility? (aka default, package, friendly)

What is protected visibility?

file:types/visibility/x/A.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
package types.visibility.x;
public class A {
  private int x0;
  int x1;
  protected int x2;
  public int x3;

  int fx(A that) {
    return this.x0 + this.x1 + this.x2 + this.x3
        + that.x0 + that.x1 + that.x2 + that.x3;
  }
}

class B {
  int gx(A that) {
    return that.x0 + that.x1 + that.x2 + that.x3;
  }
}

file:types/visibility/y/C.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
package types.visibility.y;
import  types.visibility.x.A;
class C extends A {
  int fy(C that) {
    return this.x0 + this.x1 + this.x2 + this.x3
        + that.x0 + that.x1 + that.x2 + that.x3;
  }
}

class D {
  int gy(A that) {
    return that.x0 + that.x1 + that.x2 + that.x3;
  }
}

Previous pageContentsNext page