Skip to content
Low Level Design Mastery Logo
LowLevelDesign Mastery

Contract and API Definitions

Master contract and API definitions - the bridge between design and implementation!

Contracts define how classes interact - they’re the agreements between components. Well-defined contracts lead to:

  • Testable code - Clear interfaces to mock
  • Flexible design - Can swap implementations
  • Clear documentation - Self-documenting code
  • Error prevention - Compile-time checks
Diagram

A contract defines:

  • What a class/method does
  • Inputs - Parameters and types
  • Outputs - Return types
  • Exceptions - What can go wrong
  • Preconditions - What must be true before
  • Postconditions - What will be true after
  1. Interface Contracts - Abstract interfaces
  2. Method Contracts - Method signatures
  3. API Contracts - REST/HTTP APIs
  4. Class Contracts - Class responsibilities
Diagram

Interface contracts define what a class must implement without specifying how.

Diagram
  1. Method signatures - What methods exist
  2. Parameter types - What inputs are expected
  3. Return types - What outputs are guaranteed
  4. Exceptions - What can go wrong
  5. Documentation - Clear description of behavior

Method contracts define the behavior of individual methods - inputs, outputs, and exceptions.

  1. Method signature - Name, parameters, return type
  2. Preconditions - What must be true before calling
  3. Postconditions - What will be true after calling
  4. Exceptions - What can go wrong
  5. Documentation - Clear description
Diagram

API contracts define how external systems interact with your system via HTTP/REST APIs.

  1. Endpoint - URL and HTTP method
  2. Request - Body, parameters, headers
  3. Response - Status codes, body format
  4. Error handling - Error codes and messages
  5. Authentication - How to authenticate
Diagram

Exceptions are part of the contract - they define what can go wrong and how to handle it.

  1. Validation Exceptions - Invalid input
  2. Business Logic Exceptions - Business rule violations
  3. System Exceptions - System failures
  4. Not Found Exceptions - Resource not found

Be explicit - Clear parameter and return types
Document exceptions - What can go wrong?
Use meaningful names - Self-documenting code
Define preconditions - What must be true before?
Define postconditions - What will be true after?
Handle edge cases - Null checks, validation
Use interfaces - For abstraction and flexibility

Don’t be vague - Unclear contracts lead to bugs
Don’t forget exceptions - Document what can fail
Don’t expose implementation - Hide internal details
Don’t break contracts - Once defined, maintain them
Don’t over-complicate - Keep contracts simple

Diagram

Contracts define agreements - What classes/methods will do
Interfaces - Abstract contracts for flexibility
Method contracts - Preconditions, postconditions, exceptions
API contracts - REST/HTTP API specifications
Exception handling - Part of the contract
Documentation - Clear and comprehensive
Best practices - Explicit, documented, maintainable

When defining contracts, ensure:

  • Clear signatures - Parameters and return types
  • Documented exceptions - What can go wrong?
  • Preconditions - What must be true before?
  • Postconditions - What will be true after?
  • Well documented - Clear descriptions
  • Edge cases handled - Null checks, validation
  • Consistent - Follow same patterns
Diagram

Congratulations! You’ve now mastered all the key steps of LLD interviews:

  1. Understanding LLD Interviews - What they are and why they matter
  2. Steps in LLD Interview - The systematic approach
  3. Identifying Actors & Entities - Foundation of design
  4. Assign Responsibilities - Single Responsibility Principle
  5. Class Diagrams - Visualize your design
  6. Contract and API Definitions - Define interfaces

Before your LLD interview, make sure you can:

  • Understand the problem - Ask clarifying questions
  • Identify actors - Who uses the system?
  • Identify entities - What are the core objects?
  • Assign responsibilities - One per class
  • Create class diagrams - Visualize relationships
  • Define contracts - Clear interfaces
  • Handle edge cases - Think about errors
  • Implement cleanly - Follow your design

💡 Time to Practice!

Now that you understand the concepts, put them into practice with our interactive playground. Build UML diagrams, write code, and get AI-powered feedback.

Browse All Problems