Abstraction and public contracts
What you will learn
Abstraction describes what an object offers without exposing every implementation detail. A public contract specifies how callers may use an operation: accepted inputs, returned results, and what happens for invalid input. A stable contract lets you change the implementation while keeping callers working.
After this lesson you can:
- Specify an operation using inputs, outputs and failure behavior.
- Refactor internal code and verify the same externally observable results.
Use a separate Main.java for each example. Compile with javac Main.java and run with java Main. The code is compatible with Java 21; Java 25 execution is not yet verified.
class PickupFee { public int quote(int items) { if (items < 0 || items > 100) throw new IllegalArgumentException("items outside range"); return items == 0 ? 0 : 200 + items * 30; }}public class Main { public static void main(String[] args) { PickupFee fee = new PickupFee(); System.out.println(fee.quote(3)); System.out.println(fee.quote(0)); }}Expected output:
2900The caller needs the input rule and returned amount, not the method internals.
Specify behavior before changing implementation
The quote method below accepts an item count from 0 to 100. A valid zero count costs zero cents; otherwise the fee is 200 cents plus 30 cents for each item. It rejects counts outside that range with IllegalArgumentException and never changes object state. Those are contract details. How the method organizes validation or stores the constants is an implementation choice.
A precondition describes permitted inputs; a postcondition describes the promised result when the operation completes normally. The contract should also describe what happens if a precondition is not met, if that behavior is part of the interface. In this example a specified exception is the failure behavior. We use public for the exposed method and private for internal helpers. A public method is not necessarily a good abstraction; callers need predictable semantics as well as an accessible name. Abstraction is a design idea, not a requirement to use Java's abstract keyword.
The second implementation uses a private helper and constants, but the same method signature and behavior. Keep the tests unchanged when refactoring. Check the boundary values, not just one normal request. The two versions are separate complete programs; do not paste both PickupFee declarations into the same file. A later lesson introduces interfaces to let different classes satisfy a common type contract.
class PickupFee { private static final int BASE = 200; private static final int PER_ITEM = 30; public int quote(int items) { validate(items); if (items == 0) return 0; return BASE + PER_ITEM * items; } private void validate(int items) { if (items < 0 || items > 100) throw new IllegalArgumentException("items outside range"); }}public class Main { public static void main(String[] args) { PickupFee fee = new PickupFee(); System.out.println(fee.quote(3)); System.out.println(fee.quote(0)); }}Expected output:
2900Moving validation into a helper leaves the observable results unchanged.
Check a boundary or variation
class PickupFee { private static final int BASE = 200; private static final int PER_ITEM = 30; public int quote(int items) { validate(items); if (items == 0) return 0; return BASE + PER_ITEM * items; } private void validate(int items) { if (items < 0 || items > 100) throw new IllegalArgumentException("items outside range"); }}public class Main { public static void main(String[] args) { PickupFee fee = new PickupFee(); System.out.println(fee.quote(1)); System.out.println(fee.quote(100)); }}Expected output:
2303200Both endpoints of the positive valid range are included.
Common mistakes
Changing the zero-item price from 0 to 200 is a behavior change, not merely a refactor. Removing an exposed method can break callers even if the internal calculation remains correct. Hiding helper methods reduces accidental coupling but does not excuse a poorly documented contract.
Practice
Implement PickupFee.quote(int) with the documented zero rule, 200-cent base, 30-cent item charge and accepted range. Refactor validation into a helper without changing the outputs or rejection behavior.
Write the contract cases first: zero, 1..100, outside range. Keep the method signature stable while moving validation.
Next: Immutable value objects.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Abstraction and public contracts — knowledge check
Apply the stated Java 21-compatible contracts and predict observable behavior.
Which is a contract detail?
What is a precondition in this example?
Which refactor preserves the published behavior?
Does abstraction require the abstract keyword?
What is the best evidence that the two implementations agree on these cases?