AP Computer Science --- Haas --- Basic Interface Example

Run the following interface example in BlueJ. Make sure you understand how it works.



/**
 * This is a very simple and rather ridiculous example of an interface.
 * An interface is simply a class that has a list of methods with NO implementation.
 */
public interface Flier
{
    /**
     * All interface methods must be public and abstract, and are both by default.
     */
    public abstract String fly();   // public abstract by default
    public abstract String land();  // public abstract by default
}


public class Bird implements Flier
{
  public Bird() {
  }
  
  public String fly() {
      return "flap my wings";
  }
  
  public String land() {
      return "on two feet";
  }
  
  public String speak() {
      return "tweet tweet";
  }
}


public class Plane implements Flier
{
  public Plane() {
  }

  public String fly() {
      return "use jet engines";
  }
  
  public String land() {
      return "on my wheels";
  }
}


public class Superhero implements Flier, CrimeFighter
{
  public Superhero() {
  }

  public String fly() {
      return "with super powers";
  }
  
  public String land() {
      return "on my feet";
  }
  
  public String fightsWith() {
      return "with super strength"; 
  }
}  


public interface CrimeFighter
{
   String fightsWith();
}


public class flightTester 
{  
    public static void main(String[] args)
    {
        /** 
         * The same method name can be in different objects (polymorphism). 
         * During run time Java determines which method of same name to use
         * (late or dynamic binding)
         */
               
        Bird Tweety = new Bird();
        System.out.println("\nBird");
        System.out.println("Fly: " + Tweety.fly());
        System.out.println("Land: " + Tweety.land());
        System.out.println("Speak: " + Tweety.speak());
        
        // COMPILE TIME ERROR --- can NOT instantiate an interface
        Flier Bob = new Flier();
        
        // Can only use methods found in Flier, but uses the implementations found
        // in Bird.  
        Flier Polly = new Bird();
        System.out.println("\nBird");
        System.out.println("Fly: " + Polly.fly());
        System.out.println("Land: " + Polly.land());

        // COMPILE TIME ERROR --- compile time error because speak method  is not in Flier
        System.out.println("Speak: " + Polly.speak());

        // Flier can be cast to subclass
        System.out.println("Speak: " + ((Bird)Polly).speak());
        
        Flier jet = new Plane();
        System.out.println("\nPlane");
        System.out.println("Fly: " + jet.fly());
        System.out.println("Land: " + jet.land());
        
        // RUNTIME ERROR --- Flier can NOT cast to wrong subclass
        System.out.println("Speak: " + ((Bird)jet).speak());

        Superhero Superman = new Superhero();
        System.out.println("\nSuperhero");
        System.out.println("Fly: " + Superman.fly());
        System.out.println("Land: " + Superman.land());
        System.out.println("Fights: " + Superman.fightsWith());
    }
}