Polyglot CheatSheet - Interface

Updated: 2021-11-19

C++

C++ does not have a formal contruct for interfaces, it is achieved by defining a class with only pure virtual methods.

class Foo {
public:
  virtual int bar(int a) = 0;
};

class FooImpl : public Foo {
  // ...
}

Rust

Rust uses trait to define methods that classes can implement.

trait Foo {
    fn bar(a: isize) -> isize;
}

struct FooImpl;

impl Foo for FooImpl {
    fn bar(a: isize) -> isize {
        return a;
    }
}

Java

Java uses interface keyword to define interfaces.

public interface Foo {
  public int bar(int a);
}

class FooImpl implements Foo {
  public int bar(int a) {
    return a;
  }
}

Starting from Java 8, you can implement default methods in interfaces; starting from Java 9, you can add private methods in interfaces.