Visibility

                                            Applet      Visible.java      D.java      I.java

// Classes D.class, H.class, I.class are a package called "mypackage"
// and are located in subdirectory mypackage
import mypackage.*;
import java.awt.*;
import java.applet.*;
import javax.swing.*;

class C extends D {
   C() {}
   public int bn(B b) { return b.n; }
   public int an(A a) { return a.n; }
   public int Bs()    { return B.s; }
   public int Dp()    { return p;   }
}

class A {
   int n;
   static int s = 19;

   A(int x) { n = x; }
   int an() { return n; }
   int Bs() { return B.s; }
}

class B extends A {
   static int s = 21;
   B(int x) { super(x); }
   public int bn() { return n; }
}

public class Visible extends Applet {
   JTextArea text;
   public void init () {
      add(text = new JTextArea(30,30));
      
      A a = new A(10);
      B b = new B(12);
      C c = new C();
      D d = new D(14);
      //H h = new H();                      // H not accessible
      I i = new I();

      //  Visibility of just variable
      text.append("\nVisibility of just one variable\n");
      text.append("a.an():\t"+a.an()+"\n"); // 10 - same class
      text.append("b.bn():\t"+b.bn()+"\n"); // 12 - super class
      text.append("d.dn():\t"+d.dn()+"\n"); // 14 - across package, same class
      //text.append("d.p:\t"+d.p+"\n");     // protected var not accessible
      //text.append("A.n:\t"+A.n+"\n");     // n is not a class (static) variable
      //text.append("i.i:\t"+i.i+"\n");     // variable i is not accesible
      text.append("i.ia:\t"+i.ia+"\n");     // 66 - variable is public

      //  Visibility through objects
      text.append("\nVisibility through objects\n");
      text.append("a.n:\t"+a.n+"\n");         // 10 - same class
      text.append("b.n:\t"+b.n+"\n");         // 12 - super class
      text.append("c.an(a):\t"+c.an(a)+"\n"); // 10 - other class               
      text.append("c.bn(b):\t"+c.bn(b)+"\n"); // 12 - other class
      text.append("c.Dp():\t"+c.Dp()+"\n");   // 101 - protected var access
      //text.append("d.n:\t"+d.n+"\n");       // NA - across package, same class
      //text.append("i.Ii():\t"+i.Ii()+"\n"); // not method matching Ii in I
      text.append("i.Ii2():\t"+i.Ii2()+"\n"); // 77 - public method in package

      // Visibility of static variables
      text.append("\nVisibility of static variables\n");
      text.append("A.s:\t"+A.s+"\n");         // 19 - same class
      text.append("a.Bs():\t"+a.Bs()+"\n");   // 21 - super class
      text.append("c.Bs():\t"+c.Bs()+"\n");   // 21 - other class
      // text.append("D.s:\t"+D.s+"\n");      // NA - across package,same class
     
      //Fred visible
      text.append("\nFred visible\n");
      text.append("b.an():\t"+b.an()+"\n");   // 12
      text.append("b.Bs():\t"+b.Bs()+"\n");   // 21
   }
}