CS 535: Object-Oriented Programming & Design |
---|
interface Door { public void open(); public void close(); } class CarDoor implements Door { public void open() { System.out.println( "Enter the car" ); } public void close() { System.out.println( "Look out, closing the door"); } } class TestDoor { public static void main( String[] args ) { Door automatic = new CarDoor(); automatic.open(); CarDoor direct = new CarDoor(); direct.open(); } }
interface Door { public void open(); public void close(); } interface LockableDoor extends Door { public void lock(); public void unlock(); } class CarDoor implements LockableDoor { private boolean isLocked = false; public void open() { if ( !isLocked) System.out.println( "Enter the car" ); } public void close() { System.out.println( "Look out, closing the door"); }
public void lock() { isLocked = true; } public void unlock() { isLocked = false; } } class TestDoor { public static void main( String[] args ) { Door automatic = new CarDoor(); automatic.open(); automatic.lock(); //Compile error LockableDoor better = (LockableDoor) automatic; better.lock(); //OK } }
interface Alarm { public boolean isAlarmed(); public void turnAlarmOn(); public void turnAlarmOff(); } class CarDoor implements LockableDoor, Alarm { private boolean isLocked = false; private boolean isAlarmOn = false; public boolean isAlarmed() { return isAlarmOn; } public void turnAlarmOn() { isAlarmOn = true; } public void turnAlarmOff() { isAlarmOn = false; } public void open() { if (isAlarmOn ) System.out.println( "Sound the alarm"); else if ( !isLocked) System.out.println( "Enter the car" ); } public void close() { System.out.println( "Look out, closing the door"); } public void lock() { isLocked = true; } public void unlock() { isLocked = false; } }
class CarPart { private int partID; private float weight; private float cost; public void aMethod() { System.out.println( "This is a car part method" ); } } class CarDoor extends CarPart implements LockableDoor, Alarm { private boolean isLocked = false; private boolean isAlarmOn = false; public boolean isAlarmed() { return isAlarmOn; } public void turnAlarmOn() { isAlarmOn = true; } public void turnAlarmOff() { isAlarmOn = false; } public void open() { if (isAlarmOn ) System.out.println( "Sound the alarm"); else if ( !isLocked) System.out.println( "Enter the car" ); } public void close() { System.out.println( "Look out, closing the door"); } public void lock() { isLocked = true; } public void unlock() { isLocked = false; } }
interface WithStatic { public static final int EXPLICIT = 42; public static int IS_FINAL = 12; public int IS_FINAL_AND_STATIC = 3; protected int COMPILE_ERROR = 4; public int NO_VALUE_COMPILE_ERROR; } class Radio implements WithStatic { public void AM() { System.out.println( IS_FINAL ); } } class Test { public static void main( String args[] ) { System.out.println( WithStatic.EXPLICIT ); System.out.println( Radio.EXPLICIT ); Radio megaBass = new Radio(); System.out.println( megaBass.EXPLICIT ); megaBass.AM(); } }
public interface Left { public void truth(); } public interface Right { public boolean truth(); } class Congress implements Left, Right { public ???? truth() { } }