Instruction for using the programs :
  1. Download SubClassTest.java , SuperClass.java , SubClass.java .
    And save them in the same folder.
  2. create a folder pic20 by
    mkdir pic20
  3. move the files SuperClass.java and SubClass.java to the folder pic20 by
    move SuperClass.java pic20
    move SubClass.java pic20
  4. Compile the programs by
    javac SubClassTest.java
    The compiler will automatically compile the other 2 files for you.
  5. To run the programs, use
    java SubClassTest
    and
    java pic20.SubClass

SuperClass.java

   1:/*
   2:create a folder pic20
   3:save the file into the folder pic20
   4:compile by javac pic20/SuperClass.java 
   5:(don't change your folder to pic20)
   6:*/
   7:
   8:package pic20;
   9:
  10:public class SuperClass {
  11:    private int iamprivate = 1;
  12:    protected int iamprotected = 2;
  13:    public int iampublic = 3;
  14:    
  15:    private void privateMethod() {
  16:        System.out.println("I am a private method in the class SuperClass.");
  17:    }
  18:    
  19:    protected void protectedMethod() {
  20:        System.out.println("I am a protected method in the class SuperClass.");
  21:    }
  22:    
  23:    public void publicMethod() {
  24:        System.out.println("I am a public method in the class SuperClass.");
  25:    }
  26:}
  27:    
  28:    

SubClass.java

   1:/*
   2:create a folder pic20
   3:save the file into the folder pic20
   4:compile by javac pic20/SubClass.java 
   5:(don't change your folder to pic20)
   6:to run the program, use
   7:java pic20.SubClass
   8:*/
   9:
  10:package pic20;
  11:
  12:/*
  13:The example illustrates how you can access the members from the superclass
  14:*/
  15:
  16:public class SubClass extends SuperClass {
  17:    public void accessTest() {
  18:        System.out.println(iamprivate);  // illegal
  19:        System.out.println(iamprotected);
  20:        System.out.println(iampublic);
  21:        privateMethod();  // illegal
  22:        protectedMethod();
  23:        publicMethod();
  24:    }
  25:    
  26:    public void method() {
  27:        System.out.println("I am a public method in the class SubClass.");
  28:    }
  29:    
  30:    public static void main(String[] args) {
  31:        new SubClass().accessTest();
  32:    }
  33:}

SubClassTest.java

   1:/* 
   2:Save the file in where the folder pic20 locate
   3:to run the program
   4:use java SubClassTest
   5:*/
   6:
   7:import pic20.*;
   8:
   9:public class SubClassTest {
  10:    public static void main(String[] args) {
  11:        SubClass sub = new SubClass();
  12:        sub.privateMethod(); // illegal
  13:        sub.protectedMethod(); // illegal
  14:        sub.publicMethod();
  15:        sub.method();
  16:    }
  17:}
  18: