Interfaces

Defines an interface, implements it in a class, and computes the area of a rectangle.

C C23

C23
#include <stdio.h>

// interface idiom: a vtable struct of function pointers only; implementing types provide the concrete functions
typedef struct {
    int (*area)(const void *self);
} ShapeVTable;

typedef struct {
    const ShapeVTable *vtable;
} Shape; // Shape* is the "interface reference", it can point to any implementing type

typedef struct {
    Shape iface;
    int width, height;
} Rectangle;

static int rectangle_area(const void *self) {
    const Rectangle *r = (const Rectangle *)self;
    return r->width * r->height;
}

static const ShapeVTable rectangle_vtable = { rectangle_area };

int main(void) {
    Rectangle r = { { &rectangle_vtable }, 3, 4 };
    Shape *s = &r.iface; // use via the interface type (dynamic dispatch)
    printf("%d", s->vtable->area(s));
    return 0;
}
Output
12

C++ C++26

C++26
#include <iostream>
class IShape { // interface: only pure virtual functions (conventionally named with an I prefix)
public:
    virtual int area() const = 0;
    virtual ~IShape() = default;
};
class Rectangle : public IShape {
    int width, height;
public:
    Rectangle(int width, int height) : width(width), height(height) {}
    int area() const override { return width * height; }
};
int main() {
    Rectangle r(3, 4);
    std::cout << r.area() << "\n";
    return 0;
}
Output
12

C# v11.0.100

v11.0.100
var r = new Rectangle(3, 4);
Console.WriteLine(r.Area());

interface IShape {
    int Area(); // declares signatures only, no implementation
}
class Rectangle : IShape {
    private readonly int width, height;
    public Rectangle(int width, int height) { this.width = width; this.height = height; }
    public int Area() => width * height;
}
Output
12

Go v1.27.4

v1.27.4
package main

import "fmt"

type Shape interface {
    Area() int
}

type Rectangle struct {
    width, height int
}

func (r Rectangle) Area() int {
    return r.width * r.height
}

func main() {
    var s Shape = Rectangle{width: 3, height: 4}
    fmt.Println(s.Area())
}
Output
12

Java v27.0.1

v27.0.1
public class Main {
    interface Shape {
        int area();
    }
    static class Rectangle implements Shape {
        int width, height;
        Rectangle(int w, int h) { width = w; height = h; }
        public int area() { return width * height; }
    }
    public static void main(String[] args) {
        Shape s = new Rectangle(3, 4);
        System.out.println(s.area());
    }
}
Output
12

JavaScript v26.13.1

v26.13.1
class Rectangle {
    constructor(width, height) {
        this.width = width;
        this.height = height;
    }
    area() {
        return this.width * this.height;
    }
}
function printArea(shape) {
    console.log(shape.area());
}
printArea(new Rectangle(3, 4));
Output
12

PHP v8.6.0

v8.6.0
interface Shape {
    public function area(): int;
}
class Rectangle implements Shape {
    public function __construct(private int $width, private int $height) {}
    public function area(): int {
        return $this->width * $this->height;
    }
}
$r = new Rectangle(3, 4);
echo $r->area();
Output
12

Python v3.15.2

v3.15.2
from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        ...

class Rectangle(Shape):
    def __init__(self, width, height):
        self.width = width
        self.height = height
    def area(self):
        return self.width * self.height

r = Rectangle(3, 4)
print(r.area())
Output
12

Rust v1.99.1

v1.99.1
trait Shape {
    fn area(&self) -> i32;
}

struct Rectangle {
    width: i32,
    height: i32,
}

impl Shape for Rectangle {
    fn area(&self) -> i32 {
        self.width * self.height
    }
}

fn main() {
    let r = Rectangle { width: 3, height: 4 };
    println!("{}", r.area());
}
Output
12

TypeScript v26.13.1

v26.13.1
interface Shape {
    area(): number; // declares signatures only, no implementation
}
class Rectangle implements Shape {
    private width: number;
    private height: number;
    constructor(width: number, height: number) {
        this.width = width;
        this.height = height;
    }
    area(): number {
        return this.width * this.height;
    }
}
function printArea(shape: Shape): void {
    console.log(shape.area());
}
printArea(new Rectangle(3, 4));
Output
12

Language differences

C: no classes — the example uses the struct + function-pointer vtable idiom (OOP-in-C), with semantics enforced by convention.

Go: interfaces are satisfied implicitly (no implements declaration; implementing all methods satisfies the interface).

JavaScript: no interfaces — relies on duck typing.

PHP: uses the interface keyword, and a class implements multiple interfaces.

Python: simulates interfaces with abc.ABC + @abstractmethod (duck typing is more common).

Rust: expresses interface abstraction with traits.

TypeScript: provides interface.