(c) Copyright 1998, 1999, 2000, 2001, 2002, 2004 by Daniel Berleant

 A Program Using Objects


Consider a program with two classes that controls a traffic light

Here it is:

public class SteppingIt
   {
   public static void main
        (String args[]){
      TrafficLight LincolnAndWelch 
        = new TrafficLight( );
     TrafficLight RockAndBlock 
        = new TrafficLight( );
     System.out.print("Intersection "+
        "LincolnAndWelch: color on "+
        "Welch is ");
     System.out.println(
        LincolnAndWelch.getColor( ));
     LincolnAndWelch.stepLight( );
     System.out.print("Intersection "+
        "LincolnAndWelch: color on Welch is ");
     System.out.println
       (LincolnAndWelch.getColor( ));
     }
   }

class TrafficLight
  {
  private String color;
  public void stepLight( ){
    wait(30);
    changeColor( );
  }
//public void wait(int seconds)
  private void wait(int seconds)
    {// code to wait some time 
     // before e.g. changing color
    System.out.print("Waited ");
    System.out.print(seconds);
    System.out.println(" seconds.");
    }
  public String getColor( ){
    return(color);}
//public void changeColor( )
  private void changeColor( ){
     if (color=="Red") color="Green";
     else color="Red";
     // One could add yellow too
    }
  TrafficLight( )
    {color="Red";}
  }

Let's go over this program step by step . . . 

Declaring a Derived Class


 

class TrafficLightDeluxe extends TrafficLight
  {
  private boolean blinking=false;
  public boolean getBlinking( ){ 
    return blinking;}
  public void toggleBlinking( ){
    if (blinking==true) 
        blinking=false;
    else {
         //if (color!="Green") blinking=true;   
         //WON'T WORK, color IS PRIVATE IN 
         //TrafficLight !!
         //INSTEAD, TRY...
         if (getColor( )!="Green") 
            blinking=true;
         }
    }
  }

 Take another look at the

 “extends” syntax…

 TrafficLightDeluxe is a derived class . . .

 . . . TrafficLightDeluxe inherits from ("extends") TrafficLight

 TrafficLight is the base class

All non-private members of TrafficLight are also 

    non-private members of TrafficLightDeluxe

Note we defined no constructor for TrafficLightDeluxe

...its default constructor uses the constructor for its base class

...we could also define its own constructor(s)...

 
import java.lang.*;
public class Traffic1{
   public static void main
       (String args[]){
     TrafficLight RockAndBlock 
        = new TrafficLight( );
     TrafficLightDeluxe LincolnAndWelch 
        = new TrafficLightDeluxe( );
     System.out.print("Intersection "+

        "LincolnAndWelch: color on Welch is ");
     System.out.println
        (LincolnAndWelch.getColor( ));
     System.out.print("Intersection "+
        "LincolnAndWelch: blinking on "+
        "Welch is ");
     System.out.println
       (LincolnAndWelch.getBlinking( ));
     }
   }

class TrafficLight
  {
  private String color;
  public void stepLight( ){
    wait(30);
    changeColor( );
  }
//public void wait(int seconds){
  private void wait(int seconds){
     // code to wait some time 
     // e.g. changing color
    System.out.print("Waited ");
    System.out.print(seconds);
    System.out.println(" seconds.");
    }

  public String getColor( ){
    return(color);}
//public void changeColor( ){
  private void changeColor( ){
    if (color=="Red") 
       color="Green";
     else color="Red";
     //Yes, I know I didn't do yellow.    }
  TrafficLight( ){
    color="Red";}
  } 
class TrafficLightDeluxe extends TrafficLight
  {
  private boolean blinking=false;
  public boolean getBlinking( ){ 
    return blinking;}
  public void toggleBlinking( ){
    if (blinking==true) 
       blinking=false;
    else {
         //if (color!="Green") blinking=true;   
         //WON'T WORK, color IS PRIVATE IN 
         //TrafficLight !!
         //INSTEAD, TRY...
         if (getColor( )!="Green") 
            blinking=true;
         }
    }
  TrafficLightDeluxe(boolean blinkInit){
    // calls TrafficLight constructor first
    blinking=blinkInit;
  }

  TrafficLightDeluxe( )
    { } // calls TrafficLight constructor 
  }

 (Note use of argument in a constructor) 


 See commented out code - do deluxe traffic lights have a color? 
Can we get to it? 







Clearly, multiple constructors with different arguments are allowed 

Note that the TrafficLightDeluxe constructor inherits the

   TrafficLight constructor

(If run, it would init to “Red”)

















   This could create problems if there were more than one

  TrafficLight constructor

                       How to say which we want run?

Solution: explicit inheritance of the base class's constructor

 

public class Traffic2{
   public static void main(String args[]){

     TrafficLight RockAndBlock 
        = new TrafficLight( );
     TrafficLightDeluxe LincolnAndWelch 
        = new TrafficLightDeluxe( );
     System.out.print("Intersection "
        "LincolnAndWelch: color on Welch is ");
     System.out.println
        (LincolnAndWelch.getColor( ));
     System.out.print("Intersection "
        "LincolnAndWelch: blinking on Welch is ");
     System.out.println
       (LincolnAndWelch.getBlinking( ));
     }
   }
class TrafficLight{
  private String color;
  public void stepLight( )
    {wait(30);
    changeColor( );
    }
//public void wait(int seconds)
  private void wait(int seconds)
    {// code to wait some time 
     // before e.g. changing color
    System.out.print("Waited ");
    System.out.print(seconds);
    System.out.println(" seconds.");
    }
  public String getColor( )
    {return(color);}
//public void changeColor( )
  private void changeColor( )
    {if (color=="Red") color="Green";
     else color="Red";
     //Yes, I know we didn't do yellow...
    }
  TrafficLight( )
    {color="Red";}
  } 
class TrafficLightDeluxe 
      extends TrafficLight
  {
  private boolean blinking=false;
  public boolean getBlinking( ) 
    {return blinking;}
  public void toggleBlinking( )
    {
    if (blinking==true) 
       blinking=false;
    else {
         //if (color!="Green") blinking=true;   
         //WON'T WORK, color IS PRIVATE IN
         // TrafficLight !!
         //INSTEAD, TRY...
         if (getColor( )!="Green") 
            blinking=true;
         }
    }
  TrafficLightDeluxe(boolean blinkInit)
    {
    super( ); 
    //runs the base constructor 
    //with the appropriate args
    blinking=blinkInit;
    }
  }

Summary so far:

Save on coding by not writing  TrafficLightDeluxe from scratch

. . . instead, inherit some code from TrafficLight

      then add new code

Software engineering principle:

   "Lines of code" (LOC) to write is a major determinant of project effort required

    Hence another software engineering principle: code reuse is good

Inheritance is a kind of code reuse

 

 

To reuse is to not have to write again

Save project time, effort, & cost!

...that's partly why OO (object orientation) is an important aspect 

  of modern software engineering

Multilevel Inheritance


The SuperLight class inherits from . . .

. . . TrafficLightDeluxe which in turn . . .

. . . inherits from TrafficLight

 


 public class Traffic3
   {
   public static void main(String args[])
     { 
     TrafficLight RockAndBlock 
        = new TrafficLight( );
     TrafficLightDeluxe LincolnAndWelch 
        = new TrafficLightDeluxe( );
     SuperLight LincolnAndWelch
        = new SuperLight( );
     System.out.print("Intersection"+
        "LincolnAndWelch: color on Welch is ");
     System.out.println
        (LincolnAndWelch.getColor( ));
     System.out.print("Intersection"+
        "LincolnAndWelch: blinking on Welch is ");
     System.out.println
        (LincolnAndWelch
            .getBlinking( ));
     }
   }

class TrafficLight
  {
  private String color;
  public void stepLight( )
    {wait(30);
    changeColor( );
    }
//public void wait(int seconds)
  private void wait(int seconds)
    {// code to wait some time before 
     // e.g. changing color
    System.out.print("Waited ");
    System.out.print(seconds);
    System.out.println(" seconds.");
    }
  public String getColor( )
    {return(color);}
//public void changeColor( )
  private void changeColor( )
    {if (color=="Red") 
        color="Green";
     else color="Red";
     //Yes, I know I didn't do yellow    }
  TrafficLight( )
    {color="Red";}
  }
class TrafficLightDeluxe 
      extends TrafficLight
  {
  private boolean blinking=false;
  public boolean getBlinking( ) 
     {return blinking;}
  public void toggleBlinking( )
    {
    if (blinking==true) 
       blinking=false;
    else {
         //if (color!="Green") blinking=true;   
         //WON'T WORK, color IS PRIVATE IN
         //TrafficLight !!
         //INSTEAD, TRY...
         if (getColor( )!="Green") 
            blinking=true;
         }
    }
  TrafficLightDeluxe
     (boolean blinkInit)
    {
    super( ); //runs the base 
              //constructor with the 
              //appropriate args
    blinking=blinkInit;
    }
  } 
class SuperLight 
      extends TrafficLightDeluxe
  {
  // The superLight model 
  // comes with built in computerized
  // traffic sensors, traffic prediction
  // capability, and direct
  // phone lines to all major 
  // auto insurance companies
  boolean computerControlSystem=true;
  }

 Inheritance can even form a tree . . . 

 

 

One can also have several unrelated base classes

(TrafficLight, FourWheelCar, Intersection, etc.)  

Another example: applets in Java

Each applet extends the applet base class

 

 

A Few Review Notes

A class definition automatically has a default constructor

 Thus TrafficLight( ) exists even if not declared

  A derived class constructor automatically calls its 

            super class constructor

      You can also say so by calling super( );

      You can specify a different super(...) by passing in args

      Any super( )'s with args do not exist automatically

         - you must declare them if you want them (in the base class)

         - if so, super( ) with no args will no longer exist automatically   

                 - you must define it if you want it