Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Software Design & Development · Grade 6-8

Interface

A software interface is the visible contract that tells other parts of a program how to interact with a module, function, or system.

I={inputs,outputs,constraints}

Quick Answer

A software interface is the visible contract that tells other parts of a program how to interact with a module, function, or system. It lists what you may send in, what you get back, and the rules, such as allowed values and what happens on an error, while hiding how the work is done inside. Use one wherever one part of a program, or one programmer, depends on another: as long as the interface stays the same, the inside can be rewritten without breaking anything that uses it.

Why This Matters

Interfaces are what let five people build one app: each person codes behind an agreed interface, and the pieces fit at the end. They also make change safe, because a slow function can be swapped for a fast one that accepts and returns the same things. When an interface is vague or exposes internal details, a change inside one module can break code elsewhere that nobody remembered depended on it.

Intuitive Explanation

Consider a function your team shares: average(scores). Its interface says: give it a list of numbers; it returns their mean; if the list is empty it returns 0 instead of crashing. That is everything a caller needs. Whether the function adds the numbers with a loop or a built-in sum is hidden. You can call average([80, 90, 70]) and get 80 without ever seeing the code.

The hiding is the point. Suppose the author later rewrites average to handle a million scores faster. Every call to average([80, 90, 70]) still returns 80, so no other code has to change. But if the author changes the interface itself, say the function now needs a second input for the number of decimal places, every call written as average(list) breaks at once. An interface is a promise, and breaking the promise means updating every caller.

A user interface is something else. The buttons and screens of an app are the meeting point between a person and the program; a software interface is the meeting point between two pieces of code, and no user ever sees it. The same word is used because both are boundaries. Unit tests are different again: they check that the code behind an interface keeps its promise, while the interface is the promise itself.

Think of it like this. An interface works like a restaurant menu: it lists what you can order and what will arrive, and you never see the kitchen. The chef can change the oven or the recipe and your order still comes as described. Change the menu itself, though, and regulars who order their usual dish are suddenly stuck.

Core idea. Good interfaces make modules easy to use, test, replace, and understand.


When to Use

Look at the interface when a problem asks how two modules or functions connect, what a function takes and returns, what a caller may rely on, or what breaks when a function's inputs change. Clues: parameters, return values, an API, 'other code calls this'. If the question is about buttons and screens a person uses, think User Interface; if it is about splitting a program into parts in the first place, that is modular design; checking the promised behaviour is Unit Testing.

Tip. Ask: Does one part of a program use another only through the inputs, outputs and rules it publishes, without seeing inside?

How to Recognize It

Separate what other code is allowed to know about a module from how the module does its work inside.

  1. Is the question about what you send to a module and what you get back, not how it works inside?

    Yes means the interface. A question about the hidden steps, such as how an average is calculated, is about the implementation, which the interface keeps out of sight.

  2. Is the interface something a person taps, clicks or reads on a screen?

    Yes means it is a User Interface. A software interface is used by other code, not by a person.

  3. Would changing this part force the code that uses it to change too?

    Yes means it belongs to the interface, like a function's name or its parameters. If only hidden steps change, the interface stays the same and callers are safe.

  4. Is the question about how a big program is split into separate parts?

    Yes means it is about Modular Design; interfaces are the agreed connections between those parts.

Interface vs User Interface vs Modular Design vs Abstraction

These ideas all hide details or split a program into parts, and two of them share a name. Ask who uses the boundary, other code or a person, and whether you mean the connection itself, the way the program is split, or the habit of hiding details.

Interface

Meaning
A software interface is the published set of inputs, outputs and rules that other code uses to work with a module.
Key test
Other code uses it; the inside can change while it stays fixed.
Example
get_average(scores) takes a list of numbers and returns one number, or 0 for an empty list.

User Interface

Meaning
A user interface is what a person sees and touches to use a program, such as buttons, menus and text boxes.
Key test
A person uses it, not other code.
Example
A Play button, a volume slider and a search box on a music app's screen.

Modular Design

Meaning
Modular design splits a program into separate parts, each with its own job, that connect through interfaces.
Key test
About how the whole program is divided, not one connection.
Example
A game split into graphics, sound, scoring and controls modules.

Abstraction

Meaning
Abstraction is the general habit of keeping the details that matter and hiding the rest.
Key test
A way of thinking; an interface is one thing it produces.
Example
A subway map leaves out streets and distances, keeping only the stations and lines.

Formula & Notation

I={inputs,outputs,constraints}
An interface specifies the externally visible operations of a component together with their preconditions, postconditions, and data types.

Worked Examples

Example 1 — Use a module from its interface alone

Easy

A module offers convert(amount, rate). Its interface says: amount is a number of dollars, rate is a positive number; it returns amount × rate rounded to 2 decimal places; if rate is 0 or negative it returns the message 'invalid rate'. What do convert(20, 0.92) and convert(20, 0) return?
Show the steps and the answer
  1. List the inputs and output: two numbers in, one rounded number or a message out.

    That is everything a caller may depend on; how the multiplication is coded is hidden and does not matter.

  2. Apply the stated rule to convert(20, 0.92): 20 × 0.92 = 18.4, written as 18.40.

    The interface promises the product rounded to 2 decimal places.

  3. Check convert(20, 0) against the error rule: 0 is not positive.

    The interface promises 'invalid rate'. Without that rule a caller could not tell whether a result of 0 meant 'free' or 'mistake'.

Answer: convert(20, 0.92) returns 18.40; convert(20, 0) returns 'invalid rate'.

Takeaway: You can use a module correctly from its interface alone: the inputs, the output, and what happens on bad input.

Example 2 — Same interface, new inside, then a changed interface

Standard

A game uses findHighScore(scores), whose interface says: takes a list of whole numbers, returns the largest. Version 1 sorts the list and takes the last item; version 2 walks through the list once, keeping the biggest so far. The game calls findHighScore([40, 95, 60]) in 12 places. (a) Must the 12 calls change when version 2 replaces version 1? (b) What if version 3 changes it to findHighScore(scores, count), returning the top count scores as a list?
Show the steps and the answer
  1. (a) Compare versions 1 and 2 from the outside: same input, same output.

    Both return 95 for [40, 95, 60]; the callers rely only on that, so none of the 12 calls change.

  2. (b) Compare version 3's interface: it needs a second input and returns a list instead of one number.

    Every call written as findHighScore([40, 95, 60]) is now missing an input, and code expecting 95 would get a list such as [95].

  3. Weigh the options for (b): update all 12 calls, or keep findHighScore as it was and add a new function, findTopScores(scores, count).

    Adding a new function leaves the existing contract intact, so nothing that already works breaks.

Answer: (a) No: the interface is unchanged, so all 12 calls still get 95. (b) All 12 calls break unless updated; the safer choice is to keep findHighScore and add findTopScores(scores, count).

Takeaway: Changing a module's inside is safe; changing its interface breaks every caller.

Example 3 — Find the mistake: an interface that exposes the insides

Application

Noor writes a BankAccount module for a class project. Its interface says: 'balance is the list of all transactions; to deposit, append a number to balance.' Another student withdraws by appending -500 to an account that holds 200. Find the mistake in the interface and correct it.
Show the steps and the answer
  1. Trace the effect: 200 plus the appended -500 leaves a balance of -300.

    Nothing stopped the overdraft, because callers reach straight into the internal list.

  2. Name the design error: the interface exposes how the data is stored instead of offering operations with rules.

    Any caller can break the account's rules, and if Noor ever stores transactions differently, every caller's code breaks too.

  3. Rewrite the interface: deposit(amount) adds a positive amount; withdraw(amount) subtracts it only if amount is at most the balance, otherwise returns 'insufficient funds'; getBalance() returns a number.

    The rules now live inside the module, and the transaction list is hidden.

  4. Trace again: withdraw(500) on a balance of 200 returns 'insufficient funds' and the balance stays 200.

    Under the new contract, outside code cannot make the bad withdrawal.

Answer: Hide the transaction list and offer deposit(amount), withdraw(amount) (refused when amount is more than the balance) and getBalance(). Withdrawing 500 from 200 is then refused, and the balance stays 200.

Takeaway: Expose operations with rules, not the data a module keeps inside.

Where You'll See It

  • A vending machine's buttons and slot

    The buttons, coin slot, and dispensing tray are the interface: you press a code and get a snack without knowing how the machine stores or releases it. The interface is the contract; the internal mechanism is hidden.

  • A wall power outlet

    Any device with the right plug works in the outlet because the shape and voltage are a fixed interface; you don't need to know how the power plant works to plug in your charger.

  • Calling a function someone else wrote

    When you use a function like max(a, b), its interface tells you the inputs and the output; you can use it correctly without reading the code inside it.

Common Mistakes

  1. The slip: An interface should expose the internal variables and helper steps so other code can use them directly.

    Why it's tempting: It feels powerful and convenient to let other modules reach inside and grab whatever they need.

    Instead: An interface should hide implementation details and expose only inputs, outputs, and rules; exposing internals creates coupling that breaks other code whenever you change how the module works inside.

  2. The slip: You can freely change an interface's inputs or outputs since it's your module.

    Why it's tempting: It's your code, so changing a parameter feels harmless.

    Instead: Changing a published interface breaks every piece of code that depends on it; the contract must stay stable or all callers must be updated together.

  3. The slip: An interface is the same thing as the user interface (the screen).

    Why it's tempting: Both use the word 'interface,' and the screen is the interface a person sees.

    Instead: A software interface is the contract between code modules (inputs, outputs, rules), not the visual UI a human clicks on.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. get_grade(score) accepts a whole number from 0 to 100 and returns 'A' for 90 and up, 'B' for 80 to 89, and 'C' otherwise. What does it return for 95, 80 and 79? Which inputs break the contract?

    Hint: Check each score against the ranges. What does the contract allow?

  2. Write the interface for a function that turns minutes into hours and minutes, so 135 becomes 2 hours 15 minutes. Give its input, output and one error case.

    Hint: Name the type and range of what goes in, and the shape of what comes out.

  3. A Timer module offers start(), stop() and elapsed_seconds(). Its author changes the internal storage from milliseconds to microseconds, and elapsed_seconds() still returns seconds. How many lines in other modules must change?

    Hint: Did anything other modules can see change?

  4. send_message(to, text) is called in 8 places. Its author changes it to send_message(text, to). How many calls must be updated, and what happens to a call that is missed?

    Hint: Every caller depends on the order of the inputs.

  5. A teammate asks for 'the interface of the login module'. Do they want the screen layout with the username box and button, or the function names, inputs and outputs?

    Hint: Which kind of interface is used by other code?

  6. A Bank module lets other modules read and change its internal list of balances directly. A student says: 'That's fine, it's faster than calling deposit().' What is the problem?

    Hint: What rules does deposit() enforce that direct changes skip?

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

What is an API?

API stands for application programming interface: the interface one program offers to others. A weather service's API might let your code send a city name and get back the temperature, without seeing how the forecast is made. Phones, games and websites rely on many APIs at once, for maps, payments and logins. The API's documentation is how programmers learn what they can send and what comes back.

What does interface mean in Java?

In Java and some other languages, interface is also a reserved word for a list of methods a class promises to provide. A class that implements Comparable must have a compareTo method, so any code that sorts things can rely on it. It is the same idea made official: a written contract saying what can be called, with the code that does the work supplied separately by each class.

How can an interface change without breaking everything?

Add instead of replacing. A new optional parameter, such as get_average(scores, round_to=1), keeps every old call working, because the default value fills in when it is left out. When something must be removed, teams first mark it as deprecated, leave it working for a while with a warning, and remove it in a later version once callers have moved over.

Learning Path

← Before

Modular Design
Interface

You are here

You should know Modular Design: splitting a program into self-contained parts. An interface adds the contract at each part's edge, stating what goes in, what comes out and the rules, while hiding the inside. Next, Unit Testing checks that each part keeps its interface's promises, and User Interface applies the same boundary idea where a person meets the program.

See Also