Zum Hauptinhalt springen

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:

  1. Class vs object vs static
  2. Encapsulation and access
  3. Constructors and this
  4. Inheritance, super, override vs overload
  5. 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. new stamps out a heap object.

static belongs 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

  1. Class vs object vs singleton bean vs static.
  2. Why constructor injection is an encapsulation choice.
  3. Override vs overload, with @Override.
  4. Why private / final methods are invisible to Spring proxies.
  5. The equals/hashCode contract and one reason not to put @Data on 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-upClear line
Getters = encapsulationOperations + hidden storage = encapsulation
Singleton = staticSingleton = one instance in the container
Overload = polymorphismPolymorphism is override / interface dispatch
@Data everywhereValues maybe; entities no
Proxy magicSubclass 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 / hashCode
  • final on 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.

Next: Week 3 Day 1 — Polymorphism