Use the static methods of an interface using the dot notation. - Java Object Oriented Design

Java examples for Object Oriented Design:Static

Description

Use the static methods of an interface using the dot notation.

<interface-name>.<static-method> 

Demo Code

  
class Person implements Walkable {
  private String name;

  public Person(String name) {
    this.name = name;
  }/*w ww.j  ava  2s.  c o  m*/

  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.");
  }
}


interface Walkable {
  // An abstract method
  void walk();

  // A static convenience method
  public static void letThemWalk(Walkable[] list) {
    for (int i = 0; i < list.length; i++) {
      list[i].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("c");

    Walkable.letThemWalk(w);
  }
}

Result


Related Tutorials