DEV Community

Cover image for Interface Usage Golang vs Java
VibhorDubey
VibhorDubey

Posted on

Interface Usage Golang vs Java

While experimenting with interfaces in Java and Golang, I noticed some fundamental differences in how each language approaches abstraction. This post summarizes those insights for anyone curious about both worlds.

A. Definition and Declaration

In Java:

  • An interface is an explicit construct declared using the interface keyword.
  • Classes must explicitly declare that they implement an interface using the implements keyword.
interface Shape {
    void draw();
}

class Circle implements Shape {
    public void draw() {
        System.out.println("Drawing Circle");
    }
}

Enter fullscreen mode Exit fullscreen mode

In Go:

type Shape interface {
    Draw()
}

type Circle struct{}

func (c Circle) Draw() {
    fmt.Println("Drawing Circle")
}
Enter fullscreen mode Exit fullscreen mode

An interface is also declared using the interface keyword, but implementation is implicit.

A type (struct) implements an interface automatically if it defines all the methods in that interface.

👉 Key difference:
In Java, a class must declare that it implements an interface.
In Go, a type implements an interface implicitly — no need to declare it.

B. Method Implementation and Type Checking

Java:

  • Statically checked at compile time.
  • If a class claims to implement an interface but doesn’t define all methods, compilation fails.
  • Strongly tied to inheritance hierarchy.

Go:

  • Also statically checked, but duck typing applies.
  • As long as a type has all methods required by an interface, it’s valid.
  • Promotes composition over inheritance.

C. Multiple Interfaces and Composition

Java:

  • A class can implement multiple interfaces.
  • Interface composition is explicit (using extends).
interface A { void a(); }
interface B { void b(); }
interface C extends A, B { void c(); }
Enter fullscreen mode Exit fullscreen mode

Go:

  • Interfaces are composable — you can embed one interface into another.
  • This is done by embedding rather than inheritance.
type A interface { a() }
type B interface { b() }
type C interface {
    A
    B
    c()
}
Enter fullscreen mode Exit fullscreen mode

👉 Go favors composition, while Java relies on inheritance.

D. Default Methods

Java:

  • Since Java 8, interfaces can have default and static methods with implementation.
interface Shape {
    default void info() {
        System.out.println("I am a shape");
    }
}
Enter fullscreen mode Exit fullscreen mode

Go:

  • Interfaces cannot have any implementation — they are purely abstract.
  • Behavior must be defined on concrete types.

E. Nil and Zero Values

Java:

  • null can represent an interface reference with no object.

Go:

  • The zero value of an interface is nil.
  • If an interface variable is nil, calling a method on it will panic.

F. Performance and Memory

Java:

  • Uses JVM vtables for dynamic dispatch.
  • Garbage collection handles object lifetime.

Go:

  • Uses interface value pairs (type + data pointer).
  • Interface dispatch is efficient but slightly slower than direct method calls.

Hoping this article helped in understanding the nuances of interface in Java & Golang.

Happy Coding - onwards and upwards 🚀

Top comments (0)