1.0 Basics of OOP with java
22/05/25
10 mins
Object-Oriented Programming (OOP) is a core concept in Java. If you're just starting your Java journey, learning OOP is one of the most important things you'll do. It helps you organize your code and think about problems the way we see things in the real world — using objects and their behavior.
Let’s simplify things and go step-by-step using only the official Java documentation from dev.java.
What Is OOP?
OOP is a way of writing programs using objects — things that have state (data) and behavior (actions). Java is completely built around this idea.
Imagine a bicycle. It has:
- State → speed, gear, pedal rate
- Behavior → change speed, shift gear, apply brakes
In OOP, this real-world concept turns into code using classes and objects.
Classes and Objects
A class is like a blueprint. It defines what data and actions an object should have.
Here’s an example from the official Java tutorial:
class Bicycle { int cadence = 0; int speed = 0; int gear = 1; void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; } void speedUp(int increment) { speed += increment; } void applyBrakes(int decrement) { speed -= decrement; } void printStates() { System.out.println("cadence:" + cadence + " speed:" + speed + " gear:" + gear); } }
This class defines what a Bicycle can do. To use it, you create an object (a real-world instance of the class):
Bicycle bike1 = new Bicycle();
You can create as many bikes as you want, and they’ll all have their own speed, cadence, and gear settings.
Inheritance
Inheritance means one class can use the data and actions from another class. It allows code reuse and creates a relationship between classes.
Example:
class MountainBike extends Bicycle { // This class can use methods and variables from Bicycle }
So, MountainBike
automatically gets everything that's defined in Bicycle
, unless we decide to change or add something new.
Interfaces
An interface is like a contract. It defines which actions a class should perform, but not how they’re performed.
For example:
interface Bicycle { void changeCadence(int newValue); void changeGear(int newValue); void speedUp(int increment); void applyBrakes(int decrement); }
Any class that wants to use this interface must implement all of its methods:
class ACMEBicycle implements Bicycle { // It must define each method from the interface }
This allows different types of bicycles to work in the same way, even if they’re built differently on the inside.
Packages
As your project grows, your files can get messy. Java uses packages to organize related classes and interfaces together.
Think of a package like a folder — it helps you group similar files and avoid name conflicts.
Java also offers built-in packages as part of the Java API. These are ready-made tools to help you work with everything from networking and graphics to data structures and math.
Final Thoughts
Object-Oriented Programming is at the heart of Java. By understanding:
- Classes and objects
- Inheritance
- Interfaces
- Packages
…you’re learning how to write cleaner, smarter, and more reusable code.
Whether you're building a bike simulation or a complex system, thinking in terms of objects helps you break down the task — just like in real life.
Happy coding!