Generics and templates 00:00:00 Catch the error before the worker runs Your goal is to write a single reusable queue that preserves the type of what goes in, explain which mistakes a type checker catches, and recognize a business rule that types alone cannot enforce. A worker takes the next order and prints its order number. Earlier, another component put the text word hello into the queue. Where would you want to discover the mistake: when the worker reads it, or when somebody first tries to put it in? At the attempted insertion. A generic queue parameterized with Order connects the argument to enqueue with the result of dequeue. In a statically checked caller, putting a string in that queue is rejected. The worker can then read an order without guessing or casting. 00:00:53 Fill the blank tag on one jar design Imagine making three collection jars from one drawing. Each drawing has an empty spot for a label. Fill the first with beetle, the second with snail. The jar maker has not invented a new way to open lids. The filled-in label now tells the collector what can go into this particular jar. A collector arrives with a snail. At the beetle jar, the attendant refuses it. Later, somebody opening that jar expects a beetle. The attendant represents static checking at the insertion site. In ordinary Java generic code the plastic jar itself is not a runtime guard that examines every incoming object. The same label accepts a beetle from the wrong colony. Whether that beetle belongs to today's collection is a separate rule. That's the lesson in miniature: a type can constrain the shape of data while a domain check decides whether this particular value belongs here. 00:01:49 Walk two inputs through an order queue Our store enqueues order seventeen, then order forty-two. The queue holds two orders. The first dequeue yields order seventeen, and forty-two stays at the front. Change the element type to Ticket and the queue algorithm is identical. The ticket consumer receives a ticket, not a type that it must discover at runtime. Now dequeue forty-two too. The queue's element type says Order. Does that promise there will always be an order ready on the next call? Decide what an empty queue should return before you look at the answer. No. The element type describes an item that exists; it says nothing about the number of items. A nonblocking try-dequeue might return an optional order. A blocking take might wait, with its own cancellation rule. Choose based on what the worker needs, then write a test for the empty case. 00:02:52 Order two invoices in two different ways Try a second generic operation. Invoice A is twelve dollars and due Friday. Invoice B is twenty dollars and due Monday. By amount, A comes first. By due date, B comes first. A sorting routine can work with either rule if the caller passes a comparator for invoices. An ordinary queue does not need its elements to be comparable, so don't impose that bound. A maximum function does need some way to compare two elements. Require an ordering capability or accept a comparison function. If all a function needs is to print one label, a small label-printing interface may be clearer than a type parameter. A JSON message arrives from outside your program with no amount at all. Calling an order queue generic does not turn that message into an order. Validate the payload and build a genuine order at the boundary before you enqueue it. Static checking is about typed program expressions, not arbitrary bytes from the network. 00:03:53 Defend the API design Suppose the interviewer asks for a repository reusable for orders and customers. What type relationship would you preserve between lookup and the entity it returns, and how would you distinguish a genuine missing record from a record of the wrong kind? Parameterize the repository by entity type, and by key type if useful. Looking up an order identifier yields an optional order. A missing identifier yields no entity. Give the customer repository its own type, then check an invalid call at compile time and a missing valid identifier in a runtime test. When a worker or collection returns the same kind of thing that a caller supplied, reach for a type parameter. When an algorithm needs a particular operation, require just that capability. For emptiness, invalid external data and business rules, add explicit runtime behaviour. Each promise now has an owner.