VehicleDemo.java.html

  1  public class VehicleDemo
  2  {
  3     public static void process(Vehicle v, String plateNumber)
  4     {
  5        // This example shows the syntax of instanceof and casting
  6        if (v instanceof Car) 
  7        {
  8           Car c = (Car) v; // Vehicle has no setLicensePlateNumber method--must cast
  9           c.setLicensePlateNumber(plateNumber);
 10        }
 11  
 12        System.out.println(v); // calls v.toString()
 13     }
 14  
 15     public static void main(String[] args)
 16     {
 17        Vehicle aCar = new Car(); 
 18        process(aCar, "XYX123"); 
 19  
 20        Vehicle aLimo = new Car(); 
 21        aLimo.setNumberOfTires(8);
 22        process(aLimo, "W00H00");
 23  
 24        process(new Motorcycle(), "MT1729");
 25     }
 26  }