Skip to content

sahastraWin/OOPS-JAVA

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


  ██████╗  ██████╗ ██████╗ ███████╗
 ██╔═══██╗██╔═══██╗██╔══██╗██╔════╝
 ██║   ██║██║   ██║██████╔╝███████╗
 ██║   ██║██║   ██║██╔═══╝ ╚════██║
 ╚██████╔╝╚██████╔╝██║     ███████║
  ╚═════╝  ╚═════╝ ╚═╝     ╚══════╝
             in  J A V A

Object-Oriented Programming — Architected, Not Just Written


Java IDE OOP Status License


"Programs must be written for people to read, and only incidentally for machines to execute." — Harold Abelson



◈ What Is This?

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).


◈ The Four Pillars — Implemented

┌─────────────────────────────────────────────────────────────┐
│                                                             │
│   🔒 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.                    │
│                                                             │
└─────────────────────────────────────────────────────────────┘

◈ Repository Structure

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

◈ Core Concepts Covered

🧱 Classes & Objects

  • Class declaration, instance variables, and methods
  • Constructors — default, parameterized, copy
  • this keyword and constructor chaining
  • Static vs instance members
  • Object lifecycle and memory model (Stack & Heap)

🔒 Encapsulation

  • 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

🧬 Inheritance

  • extends — single inheritance in Java
  • Method overriding (@Override)
  • super keyword — accessing parent members
  • Constructor chaining with super()
  • Multilevel and hierarchical inheritance trees

🎭 Polymorphism

  • Compile-time: Method overloading (same name, different signatures)
  • Runtime: Dynamic method dispatch via reference upcasting
  • instanceof checks and safe downcasting
  • Covariant return types

🪟 Abstraction

  • abstract classes — partial implementation contracts
  • interface — 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

🏛️ Design Patterns (OOP Applied)

  • 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

◈ Java OOP Quick-Reference Cheat Sheet

// ── 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
}

◈ Key Java OOP Concepts — Deep Dive

The Object Memory Model

STACK (Method Frames)                   HEAP (Object Data)
+--------------------------------+      +------------------------------+
|                                |      |                              |
|  main()                        |      |                              |
|  +--------------------------+  |      |    +--------------------+    |
|  |  dog  -------------------+--+------+--->|     Dog Object     |    |
|  +--------------------------+  |      |    +--------------------+    |
|                                |      |    |  name: "Bruno"     |    |
|                                |      |    |  breed: "Lab"      |    |
|  speak()                       |      |    +--------------------+    |
|  +--------------------------+  |      |               ^              |
|  |  this -------------------+--+------+---------------+              |
|  +--------------------------+  |      |        (Same Object)         |
|                                |      |                              |
+--------------------------------+      +------------------------------+

Abstract Class vs Interface

Feature abstract class interface
Instantiation ❌ Cannot instantiate ❌ Cannot instantiate
State (fields) ✅ Yes ⚠️ Only 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

Method Overloading vs Overriding

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

◈ Getting Started

Prerequisites

  • Java SE 23 or later — Download JDK
  • Eclipse IDEDownload Eclipse (recommended)
  • OR any Java IDE: IntelliJ IDEA, VS Code with Java Extension Pack

Clone the Repository

git clone https://github.com/sahastraWin/OOPS.git
cd OOPS

Open in Eclipse

1. 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 & Run via Terminal

# Compile all sources
javac -d bin src/**/*.java

# Run a specific class (e.g., Main)
java -cp bin Main

◈ Learning Path

Follow 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

◈ SOLID Principles Connection

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

◈ Tech Stack

Technology Version Role
Java SE 23 Language
Eclipse IDE 2024+ Development Environment
JDK OpenJDK 23 Compilation & Runtime

◈ Contributing

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-concept

Good 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

◈ Author

sahastraWin

Building understanding, one class at a time.

GitHub


"Complexity is the enemy of reliability. OOP, done right, tames both."


⭐ Star this repo if it helped you think in objects.


About

A curated collection of Java source code demonstrating the fundamental principles of Object-Oriented Programming. This repository covers everything from basic class structures to advanced concepts like Polymorphism, Abstraction, and Design Patterns, serving as a practical reference for mastering Java.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages