Object-Oriented Programming in Java

Learning Java OOP concepts through hands-on examples and practice projects. Join me as I explore the fundamentals of object-oriented programming!

4 Core Principles
6 Months Learning
5+ Practice Projects
Animal.java
// My first Animal class!
public class Animal {
    private String name;
    private int age;
    
    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public void makeSound() {
        System.out.println("Some generic animal sound");
    }
    
    public String getName() {
        return name;
    }
}

Learning OOP Fundamentals

Encapsulation

Learning to protect data using private fields and public methods. Understanding why we hide implementation details.

Inheritance

Creating parent and child classes to share common functionality. Practicing with extends keyword and method overriding.

Polymorphism

Understanding how the same method can behave differently based on the object type. Exploring interfaces and method overriding.

Abstraction

Learning about abstract classes and interfaces. Understanding how to define what classes should do without specifying how.

Let's Practice OOP Together!

BankAccount.java
public class BankAccount {
    private double balance;
    private String accountNumber;
    
    public BankAccount(String accountNumber, double initialBalance) {
        this.accountNumber = accountNumber;
        setBalance(initialBalance);
    }
    
    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            System.out.println("Deposited: $" + amount);
        }
    }
    
    public boolean withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            System.out.println("Withdrawn: $" + amount);
            return true;
        }
        return false;
    }
    
    public double getBalance() {
        return balance;
    }
    
    private void setBalance(double balance) {
        this.balance = Math.max(0, balance);
    }
}

Try It Out

Vehicle Hierarchy
// Base class
public abstract class Vehicle {
    protected String brand;
    protected int year;
    
    public Vehicle(String brand, int year) {
        this.brand = brand;
        this.year = year;
    }
    
    public abstract void start();
    public abstract double getFuelEfficiency();
    
    public void displayInfo() {
        System.out.println(year + " " + brand);
    }
}

// Derived class
public class Car extends Vehicle {
    private int doors;
    
    public Car(String brand, int year, int doors) {
        super(brand, year);
        this.doors = doors;
    }
    
    @Override
    public void start() {
        System.out.println("Car engine started with key");
    }
    
    @Override
    public double getFuelEfficiency() {
        return 25.5; // MPG
    }
}

// Another derived class
public class Motorcycle extends Vehicle {
    private boolean hasSidecar;
    
    public Motorcycle(String brand, int year, boolean hasSidecar) {
        super(brand, year);
        this.hasSidecar = hasSidecar;
    }
    
    @Override
    public void start() {
        System.out.println("Motorcycle started with button");
    }
    
    @Override
    public double getFuelEfficiency() {
        return 45.0; // MPG
    }
}

Vehicle Inheritance Demo

Select a vehicle type to see inheritance in action!

Shape Polymorphism
public interface Drawable {
    void draw();
    double calculateArea();
}

public class Circle implements Drawable {
    private double radius;
    
    public Circle(double radius) {
        this.radius = radius;
    }
    
    @Override
    public void draw() {
        System.out.println("Drawing a circle");
    }
    
    @Override
    public double calculateArea() {
        return Math.PI * radius * radius;
    }
}

public class Rectangle implements Drawable {
    private double width, height;
    
    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
    
    @Override
    public void draw() {
        System.out.println("Drawing a rectangle");
    }
    
    @Override
    public double calculateArea() {
        return width * height;
    }
}

// Polymorphic usage
public class ShapeManager {
    public void processShapes(Drawable[] shapes) {
        for (Drawable shape : shapes) {
            shape.draw(); // Polymorphic call
            System.out.println("Area: " + shape.calculateArea());
        }
    }
}

Polymorphism Visualizer

Database Abstraction
// Abstract class defining the contract
public abstract class DatabaseConnection {
    protected String connectionString;
    
    public DatabaseConnection(String connectionString) {
        this.connectionString = connectionString;
    }
    
    // Abstract methods - must be implemented by subclasses
    public abstract void connect();
    public abstract void disconnect();
    public abstract boolean executeQuery(String query);
    
    // Concrete method - common to all implementations
    public void logOperation(String operation) {
        System.out.println("[" + this.getClass().getSimpleName() + "] " + operation);
    }
}

// Concrete implementation for MySQL
public class MySQLConnection extends DatabaseConnection {
    public MySQLConnection(String connectionString) {
        super(connectionString);
    }
    
    @Override
    public void connect() {
        logOperation("Connecting to MySQL database...");
        // MySQL-specific connection logic
    }
    
    @Override
    public void disconnect() {
        logOperation("Disconnecting from MySQL database...");
        // MySQL-specific disconnection logic
    }
    
    @Override
    public boolean executeQuery(String query) {
        logOperation("Executing MySQL query: " + query);
        return true; // Simulated success
    }
}

// Concrete implementation for PostgreSQL
public class PostgreSQLConnection extends DatabaseConnection {
    public PostgreSQLConnection(String connectionString) {
        super(connectionString);
    }
    
    @Override
    public void connect() {
        logOperation("Connecting to PostgreSQL database...");
        // PostgreSQL-specific connection logic
    }
    
    @Override
    public void disconnect() {
        logOperation("Disconnecting from PostgreSQL database...");
        // PostgreSQL-specific disconnection logic
    }
    
    @Override
    public boolean executeQuery(String query) {
        logOperation("Executing PostgreSQL query: " + query);
        return true; // Simulated success
    }
}

Database Abstraction Demo

Java OOP Code Playground

Main.java

Output

Click "Run Code" to execute the Java code and see the output here!

// This will show:
// - Animal sounds (polymorphism)
// - Bank account operations (encapsulation)
// - Learning notes about OOP concepts

// TIP: Try changing animal names, ages, or account balances!
// The code parser is flexible and will adapt to your changes.

Ready when you are! 🚀

My Learning Projects

Simple Text-Based Game 🎮

My first game using basic OOP! Created different character classes that inherit from a Player class. Learning about constructors and method overriding.

Classes Inheritance Methods
✅ Working demo available below!

Student Grade Calculator 📊

A console app to calculate student grades. Used encapsulation to protect student data and created methods for grade calculations. Still learning about getters/setters!

Encapsulation Methods Data Validation
✅ Working demo available below!

Pet Management System 🐶

Learning project with Dog and Cat classes extending Animal. Practiced method overriding for different animal sounds. My first attempt at polymorphism!

Inheritance Polymorphism Method Overriding
✅ Working demo available below!

Basic Library System 📚

Console application to manage books and users. Learning about class relationships and basic CRUD operations. Still working on understanding interfaces!

Classes ArrayLists Basic CRUD
✅ Working demo available below!

Try My Learning Projects!

These are working implementations of the projects I've built while learning Java OOP. Click on each one to try them out!

Simple Text-Based Game

A basic RPG with different character classes

Choose Your Character:

Welcome to my first OOP game! Select a character to start.

Student Grade Calculator

Practice with encapsulation and data validation

Add Student Grades:

Student Records:

No students added yet. Add a student to see encapsulation in action!

Pet Management System

Inheritance and polymorphism with animals

Add a Pet:

My Pets:

No pets yet! Add some pets to see inheritance and polymorphism working.

Basic Library System

Class relationships and basic CRUD operations

Manage Books:

Library Catalog:

Library is empty. Add some books to get started!