Lesson Overview
Use Java generics as a maintainable form of reuse and compare the idea with template-style generated code.
Portfolio focus: Create a tiny generic Box record.
Starter: think before typing
Before running this type-safe reuse example, find the line where the main idea becomes active. Write a prediction: what must already be true for that line to work, and what should be different after it runs? The checked run ends with `Ada scored 72`; predict how the focus line helps produce that evidence.
Learning Objectives
- Write a generic class or method.
- Explain why type parameters improve reuse.
- Recognise when generic code becomes hard to read.
- Balance reusable design against the actual task.
Learning Outcomes
- By the end of the lesson, you can write a generic class or method.
- By the end of the lesson, you can explain why type parameters improve reuse.
- By the end of the lesson, you can recognise when generic code becomes hard to read.
- By the end of the lesson, you can balance reusable design against the actual task.
Why this idea exists
Generic reuse exists because programmers often need one data shape or algorithm to work with many types without duplicating the same logic for each type.
Generics grew from the broader history of parameterised types and template-style reuse. In Java, they let collections and small utility classes preserve type information while still being reusable.
This topic fits the arc after collections and abstraction because it shows reuse with constraints. The goal is not to make everything generic, but to recognise when a type parameter removes duplication while keeping the design understandable.
Deep dive
Mechanism in this example
The important mechanism is visible around `Box<String> nameBox = new Box<>("Ada");`. Read it as a concrete move in the program, not as decorative syntax: identify what value, object, branch, call or boundary is being created at that point.
Design pressure
This topic fits the arc after collections and abstraction because it shows reuse with constraints. The goal is not to make everything generic, but to recognise when a type parameter removes duplication while keeping the design understandable.
Failure mode to watch
For Generic Reuse, deliberately disturb the assumption behind `Box<String> nameBox = new Box<>("Ada");`: use an awkward value, missing input, wrong order of calls or boundary case. The useful question is how that disturbance exposes a weakness in the type-safe reuse design.
Extension step
Extend the example by doing this: Use it with two different types. The point is to make one small change that forces you to revisit the concept, rather than adding unrelated features.
Portfolio standard
The portfolio note should not repeat the lesson wording. It should show the edited code, the run result, and your own explanation of this evidence: write one warning about over-generalising code.
Lesson visual

Type this and run it
Create GenericReuseTemplatesDemo.java, type the program, and run it before changing anything. This section is about reproducing the checked baseline.
public class GenericReuseTemplatesDemo {
public static void main(String[] args) {
Box<String> nameBox = new Box<>("Ada");
Box<Integer> markBox = new Box<>(72);
System.out.println(nameBox.value() + " scored " + markBox.value());
}
}
record Box<T>(T value) { }Build and run it with:
javac GenericReuseTemplatesDemo.java && java GenericReuseTemplatesDemoExpected baseline: Ada scored 72
Run the code in your browser
Use the editor as an experiment surface. First run the checked version, then make one small change to the part of the program that demonstrates type-safe reuse and compare the new behaviour with the reference output.
Ada scored 72Line-by-line explanation
Read the code as a sequence of responsibilities. The focus line for this lesson is Box<String> nameBox = new Box<>("Ada");; the surrounding lines prepare it, use its result or make the behaviour observable.
public class GenericReuseTemplatesDemo {This names the runnable class for the Generic Reuse example, giving the compiler and JVM one clear unit to build.
public static void main(String[] args) {This is the program entry point. In Generic Reuse, it keeps the demonstration of type-safe reuse in one traceable starting script.
Box<String> nameBox = new Box<>("Ada");This introduces nameBox as named state for Generic Reuse. Later lines can read, update, pass or print that specific value as evidence.
Box<Integer> markBox = new Box<>(72);This introduces markBox as named state for Generic Reuse. Later lines can read, update, pass or print that specific value as evidence.
System.out.println(nameBox.value() + " scored " + markBox.value());This prints nameBox.value() + " scored " + markBox.value() as the observable evidence for Generic Reuse. The output lets the learner check whether the type-safe reuse idea behaved as predicted.
}This closes the innermost Generic Reuse block, so the immediately preceding method, branch or loop has finished.
}This closes the outer Generic Reuse structure, returning the reader to the surrounding class or file.
record Box<T>(T value) { }This declares a compact immutable data carrier, useful here because Generic Reuse needs a named value with fields.
Worked example
From code to explanation
Problem: Use Generic Reuse to complete a small portfolio-quality step: Create a tiny generic Box record.
Method: Locate the line `Box<String> nameBox = new Box<>("Ada");`, explain the exact role it plays, then decide what you would change to extend the example without changing the whole program.
Reveal worked answer
The checked run should produce `Ada scored 72`. A strong answer links the result back to type-safe reuse: what was created, selected, stored, called or protected, and why that matters for the portfolio task.
Trace the program
Before: Before the key operation, identify the relevant value, object, branch or resource that the type-safe reuse concept depends on.
During: Trace `Box<String> nameBox = new Box<>("Ada");` as the Generic Reuse example executes. Say whether that operation creates data, checks a condition, calls behaviour, stores information or crosses a boundary.
After: Compare the run with the expected evidence: `Ada scored 72`.
Change: Now use it with two different types, run again, and explain the smallest reason the behaviour changed.
Common misconception
A common mistake in generic reuse is treating the example as a finished answer. For type-safe reuse, the important question is narrower: which operation carries the idea, what does it make possible, and what would break if you changed it carelessly?
Quick checks
1. In this Generic Reuse example, what is the best reason to focus on `Box<String> nameBox = new Box<>("Ada");`?
2. Which evidence is strongest after you edit and rerun this example?
How to study this lesson
For Generic Reuse, predict how type-safe reuse changes the run before you press Run.
Use the first portfolio task as your main edit: Create a tiny generic Box record.
Use the second task as your variation: Use it with two different types.
Finish with evidence, not a diary entry: Write one warning about over-generalising code.
Portfolio Practice
- Create a tiny generic Box record.
- Use it with two different types.
- Write one warning about over-generalising code.
Final self-check
Can you explain the key operation?
Explain the line identified in the quick check in one or two sentences. Your answer should say what it does before the output Ada scored 72 appears.
Can you justify the portfolio evidence?
Your evidence should include the original run, one edited run, and a short note explaining how the edit affected type-safe reuse.
Study route
Practise type-safe reuse by predicting the Java example, typing it, running it in the browser, tracing the result and saving portfolio evidence.
Next, move into Resource Lifecycle and Cleanup and carry forward one improvement from this lesson into the next program.
