In lab4, we have to use the interface, we implement 3 classes.
Uses keyword “implements” to implement.
It is similar with the previous lab, only difference is the implement part and interface.
public class MoveableApp { public static void main (String [] args){ Moveable[] dirMoveable = new Moveable[3]; dirMoveable[0] = new Plane(); dirMoveable[1] = new Car(); dirMoveable[2] = new Ship(); for (Moveable i : dirMoveable) { i.moveForward(); i.moveBackward(); i.stop(); i.moveLeft(); i.moveRight(); System.out.println("--------------------"); } } } ==================================================================== public interface Moveable { public void moveForward(); public void moveBackward(); public void stop(); public void moveLeft(); public void moveRight(); } ==================================================================== public class Plane implements Moveable{ @Override public void moveForward(){ System.out.println("Plane flying forward"); } @Override public void moveBackward(){ System.out.println("Plane taxiing backward"); } @Override public void stop(){ System.out.println("Plane landed"); } @Override public void moveLeft(){ System.out.println("Plane flying Left"); } @Override public void moveRight(){ System.out.println("Plane flying right"); } } ==================================================================== public class Car implements Moveable{ @Override public void moveForward(){ System.out.println("Car drives forward"); } @Override public void moveBackward(){ System.out.println("Car drives backward"); } @Override public void stop(){ System.out.println("Car parked"); } @Override public void moveLeft(){ System.out.println("Car turns left"); } @Override public void moveRight(){ System.out.println("Car turns right"); } } ==================================================================== public class Ship implements Moveable{ @Override public void moveForward(){ System.out.println("Ship navigates forward"); } @Override public void moveBackward(){ System.out.println("Ship navigates backward"); } @Override public void stop(){ System.out.println("Ship docked"); } @Override public void moveLeft(){ System.out.println("Ship navigates left"); } @Override public void moveRight(){ System.out.println("Ship navigates right"); } } ==================================================================== ==================================================================== ===============================