Declaring an array of an interface type - Java Object Oriented Design

Java examples for Object Oriented Design:interface

Description

Declaring an array of an interface type

Demo Code

interface Walkable {
  void walk();/*from w w  w. j a va  2  s  .  c o m*/
}

class Person implements Walkable {
  private String name;

  public Person(String name) {
    this.name = name;
  }

  public void walk() {
    System.out.println(name + " (a person) is walking.");
  }
}

class Duck implements Walkable {
  private String name;

  public Duck(String name) {
    this.name = name;
  }

  public void walk() {
    System.out.println(name + " (a duck) is walking.");
  }
}

class Walkables {
  public static void letThemWalk(Walkable[] list) {
    for (Walkable w : list) {
      w.walk();
    }
  }
}

public class Main {
  public static void main(String[] args) {
    Walkable[] w = new Walkable[3];
    w[0] = new Person("a");
    w[1] = new Duck("b");
    w[2] = new Person("d");

    Walkables.letThemWalk(w);
  }
}

Result


Related Tutorials