Topic 5. OOP Concept
OOP is a complexity management approach, not just a vocabulary list. As a system grows, code without responsibility boundaries becomes fragile. OOP principles help isolate responsibilities, stabilize contracts, and support safer evolution.

Practical OOP perspective
Instead of asking only “what is OOP?”, ask:
- Where does state live and who can modify it?
- Which public methods define collaboration between components?
- Which implementation details can change without breaking callers?
If these answers are clear, architecture is usually healthy.
Encapsulation
Encapsulation restricts direct field access and enforces state changes through validated methods.

public class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("amount must be > 0");
}
balance += amount;
}
public void withdraw(double amount) {
if (amount <= 0 || amount > balance) {
throw new IllegalArgumentException("invalid withdraw amount");
}
balance -= amount;
}
public double getBalance() {
return balance;
}
}
Benefits:
- object state cannot be corrupted by random writes,
- business rules stay centralized,
- behavior is easier to test.
Inheritance
Inheritance models is-a relationships: a child class extends shared parent behavior.

class Animal {
void speak() {
System.out.println("...");
}
}
class Dog extends Animal {
@Override
void speak() {
System.out.println("Woof");
}
}
Use when:
- hierarchy is natural and stable,
- shared contract is truly shared,
- substitution is logically valid.
Use carefully:
- if goal is only code reuse, composition (
has-a) is often more flexible.
Polymorphism
Polymorphism enables programming to interfaces while swapping implementations.

interface PaymentProcessor {
void pay(double amount);
}
class CardProcessor implements PaymentProcessor {
public void pay(double amount) {
System.out.println("Card: " + amount);
}
}
class PaymentService {
private final PaymentProcessor processor;
PaymentService(PaymentProcessor processor) {
this.processor = processor;
}
void process(double amount) {
processor.pay(amount);
}
}
Benefits:
- easier extension without rewriting service logic,
- easier mocking/testing,
- lower coupling.
Abstraction
Abstraction hides implementation complexity and exposes useful contracts.

Caller uses send(message) and does not need to know retries, serialization, networking, or logging internals.
Engineering impact:
- external code works with a clear API,
- internal implementation can evolve safely,
- each layer sees only relevant complexity.
How the four principles work together
- Encapsulation protects state.
- Abstraction stabilizes API.
- Polymorphism enables extension.
- Inheritance/composition reuses behavior.
Result: OOP gives predictable change management and better long-term maintainability.
Glossary
- bytecode
- Platform-independent compiled code that the JVM can execute.
- class
- A blueprint that defines fields and methods for objects in object-oriented programming.
- object
- A runtime instance of a class with its own state and behavior.
- method
- A named block of behavior that belongs to a class or object and can be called by code.
- field
- A variable stored inside a class or object that represents part of its state.
- constructor
- A special method-like block used to create and initialize a new object.
- package
- A namespace that groups related Java classes and helps organize code.
- interface
- A contract that defines methods a class can implement without prescribing all internal details.
- generics
- A language feature that lets code work with types as parameters while preserving type safety.
- type erasure
- The Java compiler process that removes most generic type details from runtime bytecode.
- collection
- A data structure that stores a group of elements and provides operations over them.
- list
- An ordered collection where elements have positions and duplicates are allowed.
- set
- A collection that stores unique elements and is commonly used for membership checks.
- map
- A key-value data structure where each key points to a value.
- queue
- A collection designed for processing elements in a defined order, often first-in-first-out.
- lambda
- A compact function-like expression used to pass behavior as data.
- functional interface
- An interface with one abstract method, suitable for lambdas and method references.
- stream
- A Java API for processing data sequences with operations such as map, filter, and reduce.
- optional
- A container object that represents a value that may or may not be present.
- thread
- An independent path of execution inside a running program.
- synchronization
- Coordination that protects shared data when multiple threads access it.
- heap
- The runtime memory area where Java objects are allocated.
- stack
- The runtime memory area that stores method calls, local variables, and call frames.
- garbage collector
- A JVM component that automatically frees memory occupied by objects no longer reachable.
- memory leak
- A situation where memory remains occupied because objects are still referenced but no longer useful.