Java OOP

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 24 / 50 · English

Java OOP

Bring state and behavior together.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

  • Explain the key idea behind OOP.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

Understand the idea

Object-oriented programming models concepts using objects with state and behavior. A class defines a type; each new object can hold its own field values. Start with a concrete example before memorizing the names of OOP principles.

02 / Step by step

Explore the details

What is Object-Oriented Programming?

OOP organises code around objects — bundles that combine data (attributes) and behaviour (methods) in one place, mirroring real-world things.

Think of a Car: it has attributes (brand, colour, speed) and methods (accelerate, brake). In OOP you define a Car class once, then create as many car objects as you need from that blueprint.

Class vs Object

  • Class — the blueprint / template. Defined once. Describes what attributes and methods every object of that type will have.
  • Object — a real instance created from the class using new. Each object has its own copy of the attributes.

The 4 Pillars of OOP

PillarWhat it meansJava feature
EncapsulationBundle data + methods; hide internal detailsprivate fields + public getters/setters
InheritanceA child class reuses and extends a parent classextends keyword
PolymorphismSame method name behaves differently per objectMethod overriding (@Override)
AbstractionExpose only what the caller needs; hide complexityAbstract classes & interfaces

Real-world analogy: A smartphone is a perfect OOP example — it encapsulates hardware, inherits common phone behaviour, supports polymorphic apps (one tap gesture, many app responses), and abstracts away battery management from the user.

Why OOP?

Java reference fragmentJAVA
// Define a class (blueprint)
class Car {
    String brand;
    int speed;

    void drive() {
        System.out.println(brand + " driving at " + speed + " km/h");
    }
}

// Create objects (instances)
Car car1 = new Car();
car1.brand = "Toyota"; car1.speed = 80;
car1.drive();  // Toyota driving at 80 km/h

Car car2 = new Car();
car2.brand = "BMW"; car2.speed = 120;
car2.drive();  // BMW driving at 120 km/h
  • Reusability — write a class once, create hundreds of objects from it
  • Organisation — related data and behaviour stay together
  • DRY principle — Don't Repeat Yourself: one place to change, all objects updated
  • Modularity — each class can be built, tested, and maintained independently
03 / Read and predict

A complete runnable example

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        Bicycle bike = new Bicycle();
        bike.speed = 10;
        bike.accelerate();
        System.out.println(bike.speed);
    }
}
class Bicycle {
    int speed;
    void accelerate() { speed += 5; }
}
Predict first. Then reveal the expected output / வெளியீடு
15

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

Make it your own

Practice challenge

Create a second Bicycle and print its speed without accelerating it.

Reveal the answer and reasoning / விடை

The second object starts with its own speed value, 0. Changing one object does not automatically change another.

One mistake to avoid: A class is the definition of a type. A variable holding an object reference is not the class itself.

Your learning checkpoint

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.