20-CS-694 Advanced Programming Techniques Spring 2012
Visibility

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

       Visible.java      D.java      I.java      Surprises     All lectures

Visibility

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

class A {
   int a_int;
   private int a_private_int;
   protected int a_protected_int;
   public int a_public_int;
   static int a_static_int = 100;
   private static int a_private_static_int = 101;
   protected static int a_protected_static_int = 102;
   public static int a_public_static_int = 103;

   A (int x) {
      a_int = x;
      a_private_int = x+1;
      a_protected_int = x+2;
      a_public_int = x+3;
   }
   
   int a() { return a_int; }
   int a_private() { return a_private_int; }
   int a_protected() { return a_protected_int; }
   int a_public() { return a_public_int; }
   int a_static() { return a_static_int; }
   int a_private_static() { return a_private_static_int; }
   int a_protected_static() { return a_protected_static_int; }
   int a_public_static() { return a_public_static_int; }   
   
   int B_a_static() { return B.a_static_int; }
   // Variable a_pri... in class B not accessible from class A
   // int B_a_private_static() { return B.a_private_static_int; }
   int B_a_protected_static() { return B.a_protected_static_int; }
   int B_a_public_static() { return B.a_public_static_int; }   
   // int B_a() { return B.a_int; }
   // int B_a_private() { return B.a_private_int; }
   // int B_a_protected() { return B.a_protected_int; }
   // int B_a_public() { return B.a_public_int; }
}

class B extends A {
   static int a_static_int = 110;
   private static int a_private_static_int = 111;
   protected static int a_protected_static_int = 112;
   public static int a_public_static_int = 113;

   B (int x) { super(x); }
   
   int b() { return a_int; }
   // Variable a_private_int in class A not accessible from class B
   // int b_private() { return a_private_int; }
   int b_protected() { return a_protected_int; }
   int b_public() { return a_public_int; }
   int b_static() { return a_static_int; }
   int b_private_static() { return a_private_static_int; }
   int b_protected_static() { return a_protected_static_int; }
   int b_public_static() { return a_public_static_int; }   
}

public class Visible extends Applet implements ActionListener {
   JTextArea text;
   JButton button;

   public void init () {
      setLayout(new FlowLayout());
      add(new JScrollPane(text = new JTextArea(40,64)));
      add(button = new JButton("Press Me"));
      button.addActionListener(this);
   }

   public void actionPerformed (ActionEvent evt) {  doit();  }

   public void doit () {

      A a = new A(10);
      B b = new B(12);
      // Cannot access this class from here - only public classes
      // C c = new C(14);
      D d = new D(16);

      text.append("a.a_int:\t\t\t"+a.a_int+"\n");                   // 10
      text.append("a.a_private_int:\t\tVariable in A not accessible from Visible\n");               
      text.append("a.a_protected_int:\t\t"+a.a_protected_int+"\n"); // 12
      text.append("a.a_public_int:\t\t"+a.a_public_int+"\n");       // 13
      
      text.append("a.a_static_int:\t\t"+a.a_static_int+"\n");       // 100
      text.append("a.a_private_static_int:\tVariable in A not accessible from Visible\n");

      text.append("a.a_protected_static_int:\t"+a.a_protected_static_int+"\n"); // 102
      text.append("a.a_public_static_int:\t"+a.a_public_static_int+"\n"); // 103

      text.append("\n");
      
      text.append("b.a_int:\t\t\t"+b.a_int+"\n");                   // 12
      text.append("b.a_private_int:\t\tVariable in B not accessible from Visible\n");
      text.append("b.a_protected_int:\t"+b.a_protected_int+"\n");   // 14
      text.append("b.a_public_int:\t\t"+b.a_public_int+"\n");       // 15
      text.append("b.a_static_int:\t\t"+b.a_static_int+"\n");       // 110
      text.append("b.a_private_static_int:\tVariable in B not accessible from Visible\n");
      text.append("b.a_protected_static_int:\t"+b.a_protected_static_int+"\n"); // 112
      text.append("b.a_public_static_int:\t"+b.a_public_static_int+"\n"); // 113
      
      text.append("\n");
      
      text.append("a.B_a_static():\t\t"+a.B_a_static()+"\n");       // 110
      text.append("a.B_a_private_static():\t**Variable in B not accessible from A\n");

      text.append("a.B_a_protected_static():\t"+a.B_a_protected_static()+"\n"); // 112
      text.append("a.B_a_public_static():\t"+a.B_a_public_static()+"\n"); // 113
         
      text.append("\n");

      text.append("b.a():\t\t\t"+b.a()+"\n");                       // 12
      text.append("b.a_private():\t\t"+b.a_private()+"\t**Surprise**\n");
      text.append("b.a_protected():\t\t"+b.a_protected()+"\n");     // 14
      text.append("b.a_public():\t\t"+b.a_public()+"\n");           // 15

      text.append("b.a_static():\t\t"+b.a_static()+"\n");           // 100
      text.append("b.a_private_static():\t"+b.a_private_static()+"\t**Surprise**\n"); // 101
      text.append("b.a_protected_static():\t"+b.a_protected_static()+"\n"); // 102
      text.append("b.a_public_static():\t"+b.a_public_static()+"\n");     // 103
      
      text.append("\n");

      text.append("b.b():\t\t\t"+b.b()+"\n");                       // 12
      text.append("b.b_private():\t\tVariable in A not accessible from B\n");
      text.append("b.b_protected():\t\t"+b.b_protected()+"\n");     // 14
      text.append("b.b_public():\t\t"+b.b_public()+"\n");           // 15
      
      text.append("b.b_static():\t\t"+b.b_static()+"\n");           // 110
      text.append("b.b_private_static():\t"+b.b_private_static()+"\n");// 111
      text.append("b.b_protected_static():\t"+b.b_protected_static()+"\n"); // 112
      text.append("b.b_public_static():\t"+b.b_public_static()+"\n"); // 113

      text.append("\n");      

      text.append("d.d():\t\t\t"+d.d()+"\n");                         // 16
      text.append("d.d_private():\t\tVariable in C not accessible from D\n");
      text.append("d.d_protected():\t\t"+d.d_protected()+"\n");       // 18
      text.append("d.d_public():\t\t"+d.d_public()+"\n");             // 19

      text.append("d.d_static():\t\t"+d.d_static()+"\n");             // 110
      text.append("d.d_private_static():\t"+d.d_private_static()+"\n");// 111
      text.append("d.d_protected_static():\t"+d.d_protected_static()+"\n"); // 112
      text.append("d.d_public_static():\t"+d.d_public_static()+"\n");  // 113
      
      text.append("\n");
      
      text.append("d.c():\t\t\t"+d.c()+"\n");                         // 16
      text.append("d.c_private():\t\t"+d.c_private()+"\n");           // 17
      text.append("d.c_protected():\t\t"+d.c_protected()+"\n");       // 18
      text.append("d.c_public():\t\t"+d.c_public()+"\n");             // 19

      text.append("d.c_static():\t\t"+d.c_static()+"\n");             // 100
      text.append("d.c_private_static():\t"+d.c_private_static()+"\n");// 101
      text.append("d.c_protected_static():\t"+d.c_protected_static()+"\n"); // 102
      text.append("d.c_public_static():\t"+d.c_public_static()+"\n");// 103
      
      text.append("\n");
      
      text.append("d.c:\t\t\tVariable in D not accessible from Visible\n");
      text.append("d.c_private:\t\tVariable in D not accessible from Visible\n");
      text.append("d.c_protected:\t\tVariable in D not accessible from Visible\n");
      text.append("d.c_public:\t\t"+d.c_public_int+"\n");            // 19

      text.append("d.c_static:\t\tVariable in D not accessible from Visible\n");
      text.append("d.c_private_static:\tVariable in D not accessible from Visible\n");
      text.append("d.c_protected_static:\tVariable in D not accessible from Visible\n");
      text.append("d.c_public_static:\t\t"+d.c_public_static_int+"\n");// 113
      
      text.append("\n");
   }
}