Use Clock as an example of an abstract class because ALL clocks have the same mechanism for keeping time, a necessary property for an Abstract Class, to provide students with a strong real-world mental model for abstract classes.

  • This is a good example of an abstract class because all clocks have shared functionality.
    • For example, in the code below, we assume that all classes that extend the abstract class Clock would want to have an int to store hours, minutes, and seconds, and need a tick() method.
    • The display() method is left as an abstract method for child classes to define since different clocks may use digital or analog displays.
    • public abstract class Clock{
          int h,m,s;
          boolean AM;
          public void tick(){
              s++;
              if(s==60){
                  s=0;
                  m++;
              }
              if(m==60){
                  m=0;
                  h++;
              }
              if (h==12){
                  AM = !AM;
              }
          }
          public void display();
      }

More about this tip

External Source

Interview with Elissa Redmiles.