██████╗ ██████╗ ██████╗ ███████╗
██╔═══██╗██╔═══██╗██╔══██╗██╔════╝
██║ ██║██║ ██║██████╔╝███████╗
██║ ██║██║ ██║██╔═══╝ ╚════██║
╚██████╔╝╚██████╔╝██║ ███████║
╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
in J A V A
"Programs must be written for people to read, and only incidentally for machines to execute." — Harold Abelson
This repository is a curated, hands-on reference for Object-Oriented Programming in Java — built from real source code, not slides. Every concept is demonstrated through working .java files organized to take you from class declarations all the way to polymorphic hierarchies, abstract contracts, and structural design patterns.
Built in Eclipse IDE targeting Java SE 23, this project follows standard Java project conventions (src/ for sources, bin/ for compiled bytecode).
┌─────────────────────────────────────────────────────────────┐
│ │
│ 🔒 ENCAPSULATION 🧬 INHERITANCE │
│ ───────────────── ────────────── │
│ Data hiding via "Is-A" hierarchies via │
│ access modifiers, extends keyword, │
│ getters & setters, method overriding, │
│ tight class design. super() chaining. │
│ │
│ 🎭 POLYMORPHISM 🪟 ABSTRACTION │
│ ──────────────── ───────────── │
│ One interface, Abstract classes, │
│ many forms. Runtime interfaces, hiding │
│ dispatch, method impl details behind │
│ overloading. clean contracts. │
│ │
└─────────────────────────────────────────────────────────────┘
OOPS/
│
├── src/ ← All Java source files
│ ├── [classes & objects] ← Blueprint basics, constructors, `this`
│ ├── [encapsulation] ← Access modifiers, getters/setters
│ ├── [inheritance] ← Single, multilevel, hierarchical
│ ├── [polymorphism] ← Overloading, overriding, dynamic dispatch
│ ├── [abstraction] ← abstract classes, interfaces
│ └── [design patterns] ← Structural patterns & real-world models
│
├── bin/ ← Compiled .class bytecode (Eclipse output)
├── .settings/ ← Eclipse workspace settings
├── .classpath ← Eclipse classpath (JavaSE-23)
└── .project ← Eclipse project descriptor
- Class declaration, instance variables, and methods
- Constructors — default, parameterized, copy
thiskeyword and constructor chaining- Static vs instance members
- Object lifecycle and memory model (Stack & Heap)
private,protected,public, and package-private access- Getters and setters — JavaBean convention
- Immutable objects and defensive copying
- Why encapsulation is the backbone of maintainable code
extends— single inheritance in Java- Method overriding (
@Override) superkeyword — accessing parent members- Constructor chaining with
super() - Multilevel and hierarchical inheritance trees
- Compile-time: Method overloading (same name, different signatures)
- Runtime: Dynamic method dispatch via reference upcasting
instanceofchecks and safe downcasting- Covariant return types
abstractclasses — partial implementation contractsinterface— pure behavioral contracts- Default and static methods in interfaces (Java 8+)
- Functional interfaces and the road to lambdas
- Abstract class vs Interface — when to use which
- Singleton — controlled single-instance creation
- Factory — object creation without exposing logic
- Builder — step-by-step complex object construction
- Strategy — encapsulating interchangeable algorithms
- Observer — event-driven communication between objects
// ── Encapsulation ──────────────────────────────────────────
public class BankAccount {
private double balance; // hidden state
public double getBalance() { return balance; }
public void deposit(double amt) {
if (amt > 0) balance += amt; // guarded mutation
}
}
// ── Inheritance ────────────────────────────────────────────
class Animal {
String name;
void speak() { System.out.println("..."); }
}
class Dog extends Animal {
@Override
void speak() { System.out.println("Woof!"); } // override
}
// ── Polymorphism ───────────────────────────────────────────
Animal a = new Dog(); // upcast — runtime type is Dog
a.speak(); // → "Woof!" (dynamic dispatch)
// ── Abstraction ────────────────────────────────────────────
interface Drawable {
void draw(); // contract
default void print() { // Java 8+ default method
System.out.println("Printing...");
}
}
abstract class Shape implements Drawable {
abstract double area(); // partial contract
}STACK (Method Frames) HEAP (Object Data)
+--------------------------------+ +------------------------------+
| | | |
| main() | | |
| +--------------------------+ | | +--------------------+ |
| | dog -------------------+--+------+--->| Dog Object | |
| +--------------------------+ | | +--------------------+ |
| | | | name: "Bruno" | |
| | | | breed: "Lab" | |
| speak() | | +--------------------+ |
| +--------------------------+ | | ^ |
| | this -------------------+--+------+---------------+ |
| +--------------------------+ | | (Same Object) |
| | | |
+--------------------------------+ +------------------------------+
| Feature | abstract class |
interface |
|---|---|---|
| Instantiation | ❌ Cannot instantiate | ❌ Cannot instantiate |
| State (fields) | ✅ Yes | public static final |
| Constructor | ✅ Yes | ❌ No |
| Multiple inheritance | ❌ Single only | ✅ Multiple allowed |
| Default methods | ✅ Any method | ✅ Since Java 8 |
| Use when | Sharing code + contract | Pure behavioral contract |
| Aspect | Overloading | Overriding |
|---|---|---|
| Resolved at | Compile-time | Runtime |
| Location | Same class | Parent → Child class |
| Signature | Must differ | Must match exactly |
@Override |
Not applicable | Best practice to use |
| Return type | Can differ | Must be same or covariant |
- Java SE 23 or later — Download JDK
- Eclipse IDE — Download Eclipse (recommended)
- OR any Java IDE: IntelliJ IDEA, VS Code with Java Extension Pack
git clone https://github.com/sahastraWin/OOPS.git
cd OOPS1. File → Open Projects from File System
2. Select the cloned OOPS/ directory
3. Eclipse auto-detects .project and .classpath
4. Right-click any .java file → Run As → Java Application
# Compile all sources
javac -d bin src/**/*.java
# Run a specific class (e.g., Main)
java -cp bin MainFollow this sequence for maximum comprehension:
① Classes & Objects → Understand blueprints and instances
↓
② Encapsulation → Learn to hide and protect state
↓
③ Inheritance → Build "is-a" hierarchies
↓
④ Polymorphism → Write flexible, extensible code
↓
⑤ Abstraction → Design clean contracts
↓
⑥ Design Patterns → Apply OOP to real-world architecture
This codebase naturally demonstrates the SOLID principles that emerge from good OOP design:
| Principle | Meaning | How it appears here |
|---|---|---|
| Single Responsibility | One class, one job | Focused, small classes |
| Open/Closed | Open for extension, closed for modification | Abstract classes, interfaces |
| Liskov Substitution | Subtypes must be substitutable | Correct @Override usage |
| Interface Segregation | Specific interfaces over fat ones | Lean interface definitions |
| Dependency Inversion | Depend on abstractions, not concretions | Interface-based design |
| Technology | Version | Role |
|---|---|---|
| Java | SE 23 | Language |
| Eclipse IDE | 2024+ | Development Environment |
| JDK | OpenJDK 23 | Compilation & Runtime |
Contributions that expand or sharpen the OOP coverage are welcome.
# Fork → Clone → Branch → Code → PR
git checkout -b feature/your-concept
git commit -m "Add: [concept name] with example"
git push origin feature/your-conceptGood contribution ideas:
- Additional design pattern examples (Decorator, Composite, Command)
- Java 17+ OOP features (sealed classes, records, pattern matching)
- Unit tests using JUnit 5
- Javadoc improvements
sahastraWin
Building understanding, one class at a time.
"Complexity is the enemy of reliability. OOP, done right, tames both."
⭐ Star this repo if it helped you think in objects.