Learning Java OOP concepts through hands-on examples and practice projects. Join me as I explore the fundamentals of object-oriented programming!
// 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 to protect data using private fields and public methods. Understanding why we hide implementation details.
Creating parent and child classes to share common functionality. Practicing with extends keyword and method overriding.
Understanding how the same method can behave differently based on the object type. Exploring interfaces and method overriding.
Learning about abstract classes and interfaces. Understanding how to define what classes should do without specifying how.
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);
}
}
Account: ACC-001
Balance: $1000.00
// 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
}
}
Select a vehicle type to see inheritance in action!
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());
}
}
}
// 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
}
}
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 first game using basic OOP! Created different character classes that inherit from a Player class. Learning about constructors and method overriding.
A console app to calculate student grades. Used encapsulation to protect student data and created methods for grade calculations. Still learning about getters/setters!
Learning project with Dog and Cat classes extending Animal. Practiced method overriding for different animal sounds. My first attempt at polymorphism!
Console application to manage books and users. Learning about class relationships and basic CRUD operations. Still working on understanding interfaces!
These are working implementations of the projects I've built while learning Java OOP. Click on each one to try them out!
A basic RPG with different character classes
Welcome to my first OOP game! Select a character to start.
Practice with encapsulation and data validation
No students added yet. Add a student to see encapsulation in action!
Inheritance and polymorphism with animals
No pets yet! Add some pets to see inheritance and polymorphism working.
Class relationships and basic CRUD operations
Library is empty. Add some books to get started!