Name

implements

Description

Implements an interface or group of interfaces. Interfaces are used to establish a protocol between classes; they establish the form for a class (method names, return types, etc.) but no implementation. After implementation, an interface can be used and extended like any other class.

Because Java doesn't allow extending more than one class at a time, you can create interfaces instead, so specific methods and fields can be found in the class which implements it. A Thread is an example; it implements the "Runnable" interface, which means the class has a method called "public void run()" inside it.

Examples

  • interface Dot {
      void move();
      void display();
    }
    
    class CircleDot implements Dot {
      float x = 50;
      float y = 50;
    
      void move() {
        x = x + random(-1, 1);
      }
    
      void display() {
        ellipse(x, y, 16, 16);
      }
    }
    
    class SquareDot implements Dot {
      float x = 50;
      float y = 50;
    
    
      void move() {
        y = y + random(-1, 1);
      }
    
      void display() {
        rect(x, y, 16, 16);
      }
    }
    

Related