Interfaces

Representations of intefaces for classes to implement.

C++

Rust

Java


class Interfaces {
public:
  virtual int my_method(int a) = 0;
};

class Impl : public Interfaces {
public:
  virtual int my_method(int a) {
    return a;
  }
};


trait Interfaces {
    fn my_method(a: isize) -> isize;
}

#[allow(dead_code)]
struct Impl;

impl Interfaces for Impl {
    fn my_method(a: isize) -> isize {
        return a;
    }
}


public interface Interfaces {
  public int my_method(int a);
}

class Impl implements Interfaces {
  public int my_method(int a) {
    return a;
  }
}



C++ doesn't really have interfaces. Instead they are implemented by defining a base class where all methods are purely virtual. This is purely by convention and not implemented by the language.

On the other extreme is Java where an Interface is a formal construct. Classes can implement multiple interfaces, but only inherit from a single base class.

Rust is much more like Java with traits that define methods classes can implement.