BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ OOP and types

Java language

Interfaces, inheritance and polymorphism

An interface is a contract of methods that classes can promise to provide.

๐Ÿ”Ž Unravel this example

At a glance

Mental model

A power socket standard: anything with the right plug works, and the socket does not care whether it is a lamp or a kettle.

What happens, in order
  1. 1A class that implements an interface must provide its methodsA variable of the interface type can hold any implementation, and calls go to the actual object's method at runtime (polymorphism).
  2. 2@Override asks the compiler to check that you really are overriding a method; a typo in the name becomes a compile error
  3. 3Prefer composition (holding another object) over inheritance (extends) unless the "is-a" relationship is really true; inheritance couples subclass and parent tightly
Remember
Watch out
Deep inheritance hierarchiesMissing @Override, silently creating a new method
Example (highlighted lines explain it)
public interface PaymentGateway {    Receipt charge(String account, BigDecimal amount);}public class StripeGateway implements PaymentGateway {    @Override    public Receipt charge(String account, BigDecimal amount) {        return client.charge(account, amount);    }}
  1. line 6 uses Override, the core of Interfaces, inheritance and polymorphism
Read the full explanation (about 1 min)

An interface is a contract of methods that classes can promise to provide. Code written against the interface works with any implementation, which is what lets you swap or fake parts of a program.

A mental model

A power socket standard: anything with the right plug works, and the socket does not care whether it is a lamp or a kettle.

How it works

A class that implements an interface must provide its methods. A variable of the interface type can hold any implementation, and calls go to the actual object's method at runtime (polymorphism).

@Override asks the compiler to check that you really are overriding a method; a typo in the name becomes a compile error.

Prefer composition (holding another object) over inheritance (extends) unless the "is-a" relationship is really true; inheritance couples subclass and parent tightly.

Going deeper

Interfaces allow default and static methods, which lets libraries add behaviour without breaking implementors. Programming to interfaces (for example List, not ArrayList) keeps code flexible and testable.

Under the hood

Calls through an interface are dispatched at runtime using method tables. The JIT can inline the call when only one implementation has been loaded, then deoptimise if another appears.

Common mistakes

  • Deep inheritance hierarchies.
  • Missing @Override, silently creating a new method.

How Unravel recognises it

When you paste code, Unravel looks for these signs of interfaces, inheritance and polymorphism. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Builds on this

Frequently asked questions

What is Interfaces, inheritance and polymorphism?

An interface is a contract of methods that classes can promise to provide. Code written against the interface works with any implementation, which is what lets you swap or fake parts of a program.

What are the most common mistakes with Interfaces, inheritance and polymorphism?

Deep inheritance hierarchies. Missing @Override, silently creating a new method.

How can I see Interfaces, inheritance and polymorphism in real code?

Paste your own Java into Unravel and it will point out where interfaces, inheritance and polymorphism appears, explain how it works at the detail level you choose, and draw the flow of the code around it.