DESCRIPTION:
In this experiment an interface named Movable was given, and I had to create three class CAR(), PLANE() and SHIP(). These classes will implement the interface as well as I had to create a program that will polymorphically run an array of the interface by calling each of its methods after creating an object for each one of the classes.
CODE:
public class MovableAction { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Movable[] myMovable = new Movable[3]; myMovable[0] = new CAR(); myMovable[1] = new Plane(); myMovable [2] = new Ship(); for(Movable m: myMovable){ m.moveForward(); m.moveBackward(); m. stop(); m.moveRight(); m.moveLeft(); System.out.println("-----------------------"); } } } __________________________________________________________________________ public interface Movable { public void moveForward(); public void moveBackward(); public void stop(); public void moveLeft(); public void moveRight(); } __________________________________________________________________________ public class CAR implements Movable { @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 drives left"); } @Override public void moveRight(){ System.out.println("Car drives right"); } } __________________________________________________________________________ public class Ship implements Movable { @ 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"); } } __________________________________________________________________________ public class Plane implements Movable { @ Override public void moveForward(){ System.out.println("Plane flying forward "); } @ Override public void moveBackward(){ System.out.println("Plane taxiing"); } @ Override public void stop(){ System.out.println("Plane Landing"); } @ Override public void moveLeft(){ System.out.println("Plane flying left"); } @ Override public void moveRight(){ System.out.println("Plane flying right"); } } ____________________________________________________________________
SCREENSHOTS