Week 2 Review — OOP Core
Goal
This review checks the OOP core Spring is built on: objects, hidden state, construction, inheritance, and Object.
Week 2 topics:
- Class vs object vs
static - Encapsulation and access
- Constructors and
this - Inheritance,
super, override vs overload toString,equals,hashCode
1. Week 2 big picture
Class OrderService (template)
constructor(OrderRepository)
private final OrderRepository orders
public place(CreateOrderRequest)
Spring (or a test) calls the constructor
└── one object on the heap, fields already valid
A proxy subclass may extend OrderService
└── overrides place() to start a transaction, then super.place()
If I cannot tell that story, Week 3’s interfaces and SOLID will feel like slogans.
2. Core memory sentences
The class is the blueprint.
newstamps out a heap object.
staticbelongs to the type. Instance state belongs to the object.
Encapsulation keeps the invariant inside the object.
After
new, the object is valid — or the constructor threw.
The variable type is the API. The runtime type is the implementation.
Override is runtime. Overload is compile time. Use
@Override.
Equal objects must share a hash.
A Spring bean is an object; the annotation is on the class.
3. Speak these without notes
- Class vs object vs singleton bean vs
static. - Why constructor injection is an encapsulation choice.
- Override vs overload, with
@Override. - Why
private/finalmethods are invisible to Spring proxies. - The
equals/hashCodecontract and one reason not to put@Dataon an entity.
4. Tiny code proofs
Proof A — static hide vs instance override
Write a parent/child pair with a static kind() and an instance run(). Call both through a parent-typed variable. Confirm static prints the parent, instance prints the child.
Proof B — constructor injection in a test
OrderRepository fake = new InMemoryOrders();
OrderService service = new OrderService(fake);
service.place(new CreateOrderRequest("sku", 1));
No Spring. Same constructor production uses.
Proof C — broken hashCode
Put a mutable object in a HashSet, mutate the field used by equals, then contains. Watch it fail. Then make the type a record of immutable components and repeat.
5. Common mix-ups from this week
| Mix-up | Clear line |
|---|---|
| Getters = encapsulation | Operations + hidden storage = encapsulation |
| Singleton = static | Singleton = one instance in the container |
| Overload = polymorphism | Polymorphism is override / interface dispatch |
@Data everywhere | Values maybe; entities no |
| Proxy magic | Subclass or interface implementation that overrides methods |
6. Interview drill
Open Java fundamentals:
- Overloading vs overriding
- How do you make a class immutable? (ties to encapsulation)
equals/hashCodefinalon class, method, variable- Composition vs inheritance (preview; full answer after Week 3)
7. Ready for Week 3?
I am ready if I can explain override vs overload and why a constructor-injected service is a complete object.
Week 3 is design: polymorphism, interfaces, composition, SOLID, and the same ideas inside Spring.