Skip to main content

Interface

In Turkish
arayüz
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/interface

In short

An interface is a named set of method and property signatures that a type promises to provide, without saying how those members are implemented.

What is an interface in programming?

An interface describes what a piece of code can do without saying how it does it. It lists method names with their parameters and return types, and sometimes properties, but it holds no working logic of its own. Any class that implements the interface promises to provide all of those members, so other code can rely on them being there.

Java, C#, Go, and TypeScript have an interface keyword, while Swift calls the same idea a protocol and Rust calls it a trait. In Java and C#, a class declares implements explicitly and the compiler checks that every required method is present. In Go and TypeScript, typing is structural, which means any type with the right shape satisfies the interface automatically, even if it never mentions it.

A wall power socket is a good analogy: it defines the shape of the plug and the voltage, and any device that fits can be plugged in, whether it's a lamp or a laptop charger. In code, interfaces let you swap implementations, for example a real payment service in production and a fake one in tests, which is the basis of dependency injection and mocking.

Interfaces are often confused with abstract classes. An abstract class can contain shared code and fields, and a class can usually extend only one, while an interface mainly defines a contract and a class can implement many. The same word appears in API, short for application programming interface, which applies the idea at a larger scale: a public contract that hides the internal details.

At a glance

An interface names what a type must provide without saying how: Shape promises an area() method, Circle, Square and Triangle each implement it their own way, and totalArea() works with any Shape, so a Triangle added later needs no change to it.implementstotalArea(shapes)uses only Shapeinterface Shapearea(): numberclass Circlearea() → π × r²class Squarearea() → side²class Trianglearea() → b × h / 2added later: totalArea() is unchanged
Code that depends on an interface instead of a concrete class can be tested with a stand-in and extended without being edited.

Key takeaways

  • An interface defines which methods and properties a type must have, not how they work.
  • A class can implement several interfaces but usually extends only one class.
  • Java and C# require an explicit implements; Go and TypeScript check the shape automatically.
  • Coding against interfaces makes it easy to swap implementations and write tests.

Example

Defining and implementing an interface in TypeScripttypescript
interface Shape {
  name: string;
  area(): number; // a signature only, no body
}

class Circle implements Shape {
  name = "circle";
  constructor(private radius: number) {}
  area() { return Math.PI * this.radius ** 2; }
}

// Accepts any Shape, not just Circle
function describe(shape: Shape): string {
  return `${shape.name}: ${shape.area().toFixed(2)}`;
}

Readers ask

What is the difference between an interface and an abstract class?

An interface mainly declares a contract of members a class must provide, and a class can implement many interfaces. An abstract class can also hold shared code and state, but a class can usually extend only one.

Can an interface contain code?

Traditionally no, but some languages now allow it. Java 8 and later and C# 8 and later let interfaces include default method implementations, while TypeScript interfaces describe types only and disappear completely after compilation.

What is the difference between an interface and a type in TypeScript?

Both can describe the shape of an object. Interfaces can be extended and are merged when declared twice with the same name, while type aliases can also describe unions, tuples, and primitives; for plain object shapes, either works.

See also

Free · once a week

Software and AI, explained once a week

The week's software and AI news with every term explained, plus a term to learn and a quick quiz. A two-minute read.

You confirm by email first, and one click unsubscribes. Privacy

See this week's issue

About this page

Page history

  1. Added a diagram: one interface, three classes and the code that only knows the interface.

Changes that altered what this page says. Typo fixes are not listed.

Spotted a mistake or something missing on this page?Suggest an edit

How pages are made

More

Settings