100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Interfaces

IntermediateConcept8.5K learners

An interface defines a contract of methods and properties that a class or object must implement, specifying what operations are available without dictating how they're implemented.

Definition

An interface defines a contract of methods and properties that a class or object must implement, specifying what operations are available without dictating how they're implemented.

Overview

Interfaces are a core tool for structuring object-oriented and statically typed code. Rather than describing how something works internally, an interface describes what a type can do — for example, an interface might declare that any implementing class must provide a `save()` method and a `load()` method, without specifying whether that class saves to a file, a database, or a network service. This separation of contract from implementation is what allows different classes to be used interchangeably wherever the interface is expected, a capability closely related to polymorphism. In languages like TypeScript and Java, interfaces are checked at compile time, so any class claiming to implement an interface must provide every method the interface declares, or the code won't compile. This makes interfaces a powerful tool for dependency injection: code can depend on an interface rather than a concrete class, making it easy to swap implementations — for testing, for different environments, or as requirements evolve — without changing the code that consumes the interface. Interfaces differ from abstract classes in an important way: most languages allow a class to implement multiple interfaces but inherit from only one class (including abstract classes), so interfaces are the primary mechanism for achieving something like multiple inheritance of behavior contracts. Interfaces also typically contain no implementation at all, while abstract classes can provide shared implementation alongside abstract methods that subclasses must fill in. Good interface design tends to favor small, focused contracts over large, do-everything interfaces — a principle sometimes called interface segregation — because narrow interfaces are easier to implement correctly, easier to mock in tests, and create looser coupling between components.

Key Concepts

  • Declares a contract of methods/properties without providing implementation
  • Enables polymorphism by letting different classes be used interchangeably
  • A class can implement multiple interfaces, unlike single class inheritance
  • Checked at compile time in statically typed languages like TypeScript and Java
  • Foundational tool for dependency injection and testable, decoupled code
  • Encourages designing to a contract rather than a concrete implementation
  • Small, focused interfaces are generally easier to implement and mock

Use Cases

Defining contracts for dependency injection and swappable implementations
Enabling polymorphic behavior across unrelated classes
Mocking dependencies in unit tests by implementing a test double of an interface
Designing plugin systems where multiple implementations share a contract
Structuring API client abstractions (e.g., interchangeable storage backends)
Typing function parameters and return values in TypeScript
Enforcing consistent method signatures across a codebase

Frequently Asked Questions

From the Blog