// file: Vehicle.java // author: Robert Keller // purpose: Illustrating abstract classes import java.io.*; // The base class, Vehicle, which is abstract. abstract class Vehicle { abstract String type(); public static void main(String arg[]) { Vehicle a, b; a = new Automobile(); // ok, since Automobile is a kind of Vehicle b = new Boat(); // ok, since Boat is a kind of Vehicle System.out.println(a.type()); System.out.println(b.type()); System.out.println("a " + analysis(a)); System.out.println("b " + analysis(b)); } // create a more complete analysis of this object // Traces Object o's class up the hierarchy until the root Object is reached, // as signified by the class having no parent (superClass) static String analysis(Object o) { StringBuffer b = new StringBuffer(); Class c = o.getClass(); Class parent = c.getSuperclass(); while( parent != null ) { b.append("is a " + c.getName() + ", "); c = parent; parent = c.getSuperclass(); } b.append("is an Object."); return b.toString(); } } // Vehicle // The class Automobile, a special case of Vehicle class Automobile extends Vehicle { String type() { return "Automobile"; } } // Automobile // The class Boat, a special case of Vehicle class Boat extends Vehicle { String type() { return "Boat"; } } // Boat