Source of ThreePillars.java


  1: //ThreePillars.java
  2: //Simple demo of encapsulation, inheritance and polymorphism,
  3: //the "three pillars" of object-oriented programming.

  5: import java.io.*;


  8: public class ThreePillars
  9: {
 10:     static BufferedReader keyboard =
 11:         new BufferedReader(new InputStreamReader(System.in));
 12:     static PrintWriter screen =
 13:         new PrintWriter(System.out, true);

 15:     public static void pause()
 16:     throws IOException
 17:     {
 18:         screen.print("Press Enter to continue ... ");
 19:         screen.flush();
 20:         keyboard.readLine();
 21:     }


 24:     static void displayGrossWage(Worker person)
 25:     {
 26:         screen.println(person.getName()   + " earns $" +
 27:                        person.getGrossWage() + " per week.");
 28:         //There is only one "getName()" method.
 29:         //But, "dynamic method lookup" is performed to get the correct
 30:         //version of "getGrossWage()". This is polymorphism in action.
 31:     }

 33:     public static void main(String[] args)
 34:     throws IOException
 35:     {
 36:         Worker janitor = new Worker("Fred");
 37:         janitor.setHoursWorked(35.0f);
 38:         janitor.setRateOfPay(10.25f);

 40:         Executive director = new Executive("Clive");
 41:         director.setAnnualSalary(48108.0f);

 43:         //The output from this is perhaps not that surprising ...
 44:         screen.println();
 45:         screen.println(janitor.getName()    + " earns $" +
 46:                        janitor.getGrossWage()  + " per week.");
 47:         screen.println(director.getName()   + " earns $" +
 48:                        director.getGrossWage() + " per week.");
 49:         pause();

 51:         //The output from this is perhaps more surprising ...
 52:         screen.println();
 53:         displayGrossWage(janitor);
 54:         displayGrossWage(director);
 55:         pause();
 56:     }
 57: }