DEV Community

Cover image for My Journey Learning Object-Oriented Programming Through Java
Dahami Fabbio
Dahami Fabbio

Posted on

My Journey Learning Object-Oriented Programming Through Java

When I first decided to dive into programming, I heard the term "object-oriented" tossed around constantly. Everyone seemed to agree it was important, but understanding what it really meant proved to be a fascinating challenge. Java became my gateway into this paradigm, and I wanted to share my experience learning object-oriented programming (OOP) through this powerful language.

Starting with the Basics

My journey began with simple Java syntax - variables, loops, and conditional statements. But the real "aha" moment came when I started working with classes and objects. I remember creating my first class:

public class Dog {
    // Properties (attributes)
    private String name;
    private String breed;
    private int age;

    // Constructor
    public Dog(String name, String breed, int age) {
        this.name = name;
        this.breed = breed;
        this.age = age;
    }

    // Method
    public void bark() {
        System.out.println(name + " says: Woof!");
    }
}
Enter fullscreen mode Exit fullscreen mode

Creating an actual Dog object from this blueprint and making it "bark" was surprisingly satisfying:

Dog myDog = new Dog("Buddy", "Golden Retriever", 3);
myDog.bark();  // Outputs: Buddy says: Woof!
Enter fullscreen mode Exit fullscreen mode

This simple example helped me understand the core concept: objects are instances of classes, which serve as blueprints describing both data (properties) and behavior (methods).

The Four Pillars of OOP

As my understanding deepened, I discovered that OOP in Java rests on four fundamental principles:

1. Encapsulation

Learning to hide implementation details behind public interfaces was my first big step. Java's access modifiers (private, protected, public) made sense when I realized they were protecting my objects from misuse:

public class BankAccount {
    private double balance;  // Private - can't be accessed directly

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    public double getBalance() {
        return balance;
    }
}
Enter fullscreen mode Exit fullscreen mode

By controlling access to data, I could ensure a bank account's balance could only be modified through proper methods like deposit().

2. Inheritance

Inheritance clicked for me when I started modeling real-world relationships:

public class Animal {
    protected String name;

    public void eat() {
        System.out.println(name + " is eating.");
    }
}

public class Cat extends Animal {
    public Cat(String name) {
        this.name = name;
    }

    public void meow() {
        System.out.println(name + " says: Meow!");
    }
}
Enter fullscreen mode Exit fullscreen mode

I could now create a Cat that automatically had all the capabilities of an Animal, plus its own unique behaviors - a perfect reflection of the "is-a" relationship.

3. Polymorphism

Polymorphism was perhaps the most mind-bending concept at first. The ability for objects to take different forms depending on context seemed like magic:

public class Animal {
    public void makeSound() {
        System.out.println("Some generic animal sound");
    }
}

public class Dog extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Woof!");
    }
}

public class Cat extends Animal {
    @Override
    public void makeSound() {
        System.out.println("Meow!");
    }
}
Enter fullscreen mode Exit fullscreen mode

And then using it:

Animal myPet = new Dog();
myPet.makeSound();  // Outputs: Woof!

myPet = new Cat();
myPet.makeSound();  // Outputs: Meow!
Enter fullscreen mode Exit fullscreen mode

The same method call produced different behaviors depending on the actual object type - this flexibility was revolutionary to me.

4. Abstraction

Learning to use abstract classes and interfaces helped me think at a higher level:

public interface Vehicle {
    void accelerate();
    void brake();
}

public class Car implements Vehicle {
    public void accelerate() {
        System.out.println("Car is accelerating");
    }

    public void brake() {
        System.out.println("Car is braking");
    }
}
Enter fullscreen mode Exit fullscreen mode

By focusing on what objects do rather than how they do it, I could build more flexible systems.

Real Projects Cemented My Understanding

Reading about OOP concepts was helpful, but actually building projects brought everything together. I created a simple inventory management system where Product was the parent class, with subclasses like Electronics, Clothing, and Groceries. Each had their own attributes and behaviors, but they all functioned as Products in the system.

I learned that OOP isn't just about modeling real-world objects; it's about creating clean, maintainable code that mirrors how we naturally think about problems.

Challenges Along the Way

Not everything was smooth sailing. I struggled with:

  • Design decisions: Knowing when to use inheritance versus composition
  • Class relationships: Understanding the differences between "is-a" and "has-a" relationships
  • Overengineering: Creating unnecessary classes and abstractions that complicated my code

These challenges taught me that OOP isn't a silver bullet but a tool that requires judgment and experience to use effectively.

Why Java Was a Great Teacher

Looking back, Java was the perfect language for learning OOP because:

  1. It's strictly object-oriented, forcing me to think in terms of classes and objects
  2. Its syntax explicitly states access modifiers, making encapsulation clear
  3. Its strong typing helped me understand inheritance relationships
  4. Its interface and abstract class mechanisms made polymorphism tangible

Where I'm Headed Next

Now that I've grasped the fundamentals, I'm excited to explore design patterns - proven solutions to common software design problems. I'm also looking at how modern Java features like lambdas and streams complement object-oriented programming.

OOP through Java has changed how I approach problem-solving, and while I know there's much more to learn, I'm grateful for the solid foundation it's given me in creating organized, reusable, and maintainable code.

Have you learned OOP through Java or another language? I'd love to hear about your experience in the comments below!

Top comments (0)