Object-Oriented Programming

This is the most important lesson in the entire Java course. Java is fundamentally object-oriented — everything lives inside a class . Even your main() method is inside a class. Understanding OOP is what separates beginners who can write scripts from developers who can build real applications. After this lesson, you'll think about code completely differently.

Learn Object-Oriented Programming in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

💡 Real-World Analogy: Blueprints & Houses

A class is like a blueprint for a house. It describes the rooms, their sizes, and how the wiring works — but it's not a house itself. When you build a house from the blueprint, that's an object (also called an instance). You can build many houses from one blueprint, each with different paint colors (field values) and different furniture inside.

Before OOP: Programs were long lists of instructions (procedural code). As software grew to thousands of lines, this became unmanageable. OOP lets you break problems into objects that each handle their own data and behavior — like departments in a company, each responsible for their own work.

Every class has three parts: fields (data it stores), a constructor (how to create it), and methods (what it can do). Let's build a Car class step by step:

Now let's USE this class by creating objects:

🔑 Key insight: Each object is independent . Driving myCar doesn't affect friendsCar . They share the same blueprint (class) but have their own copies of the data (fields).

Inside a method or constructor, this refers to the current object — the specific car, student, or account that the method is being called on. It's most important when parameter names match field names:

Encapsulation means hiding the internal data of an object and only allowing access through controlled methods. Think of a bank account: you can't reach into the vault directly. You go through the teller (methods) who validates your request.

🔑 Why not just make everything public? Without encapsulation, anyone could write acct.balance = -999999 . Encapsulation ensures your data stays valid by forcing all changes through methods that include validation.

Just like methods, constructors can be overloaded — you can have multiple constructors with different parameter lists, giving users different ways to create objects:

Instance members belong to each individual object (each car has its own mileage). Static members belong to the class itself and are shared by all objects.

These four principles are the foundation of object-oriented design. You've already learned the first two — the others come in the next lessons:

Hide internal details, control access via methods. Like a TV remote — you press buttons, you don't rewire circuits.

Show only essential features, hide complexity. Like driving a car — you use the steering wheel without understanding the engine.

Create new classes from existing ones. "Dog is an Animal" — dogs automatically get Animal behaviors.

Objects can take many forms. A draw() method works differently for Circle, Square, and Triangle.

By default, printing an object shows something useless like Car@1b6d3586 . Override toString() to make it human-readable:

💡 Name classes with nouns: Car , Student , BankAccount — not CarManager or DoStuff .

💡 Keep classes focused: A class should represent ONE concept. If it's doing too many things, split it up.

💡 Always override toString(): Makes debugging 10x easier when you can print objects meaningfully.

💡 Start private, open later: Begin with everything private. Make things public only when external access is actually needed.

💡 Getters without setters = read-only fields: Provide a getter but no setter for values that shouldn't change after creation (like a student's ID).

You now understand classes, objects, encapsulation, constructors, the this keyword, and static members — the foundation of ALL Java programming. Every framework, library, and API you'll ever use in Java is built on these concepts.

Next up: Inheritance — create new classes from existing ones to maximize code reuse and build logical hierarchies.

Practice quiz

What is the relationship between a class and an object?

  • They are identical
  • An object is a blueprint for a class
  • A class is a blueprint; an object is an instance built from it
  • A class is created with new

Answer: A class is a blueprint; an object is an instance built from it. A class is the blueprint; an object (instance) is a concrete thing created from that blueprint with new.

What are the three core parts of a class?

  • Fields, constructor, methods
  • Imports, loops, returns
  • Public, private, static
  • Class, object, instance

Answer: Fields, constructor, methods. A class has fields (data), a constructor (how to create it), and methods (what it can do).

What does the 'this' keyword refer to inside a method?

  • The class itself
  • The parent class
  • A static field
  • The current object the method is called on

Answer: The current object the method is called on. this refers to the current object — useful when a parameter name matches a field name.

What does encapsulation mean?

  • Making all fields public
  • Hiding internal data and controlling access through methods
  • Copying objects
  • Running code faster

Answer: Hiding internal data and controlling access through methods. Encapsulation hides internal data (e.g. private fields) and exposes controlled access via methods.

Why declare a field like balance as private?

  • So outside code can't set it to an invalid value directly
  • To make it run faster
  • So it prints automatically
  • Because private fields are static

Answer: So outside code can't set it to an invalid value directly. private forces all changes through validated methods, preventing invalid states like a negative balance.

What is wrong with: public Car(String brand) { brand = brand; }

  • Nothing
  • It won't compile
  • It assigns the parameter to itself; the field stays unchanged — needs this.brand = brand
  • It creates two objects

Answer: It assigns the parameter to itself; the field stays unchanged — needs this.brand = brand. Without this., 'brand = brand' just reassigns the parameter; use this.brand = brand to set the field.

How is a static field different from an instance field?

  • Static fields are private
  • A static field is shared by all objects; an instance field belongs to each object
  • Instance fields are shared
  • There is no difference

Answer: A static field is shared by all objects; an instance field belongs to each object. Static members belong to the class and are shared by all instances; instance fields are per-object.

In the lesson, static int totalStudents is incremented in the constructor. After creating 3 students it is 3 — what is it after creating a 4th?

  • 1
  • 3
  • 0
  • 4

Answer: 4. The static counter is shared across all instances, so a 4th student makes it 4.

Why override the toString() method?

  • To make the object run faster
  • To give the object a human-readable printed form instead of Car@1b6d3586
  • To make fields private
  • To create a constructor

Answer: To give the object a human-readable printed form instead of Car@1b6d3586. Overriding toString lets println show meaningful text instead of a default Class@hashcode string.

How do you actually create an object from a Car class?

  • Car myCar;
  • Car myCar = Car();
  • Car myCar = new Car("Toyota", 2023);
  • new myCar.Car();

Answer: Car myCar = new Car("Toyota", 2023);. You must use new with a constructor: Car myCar = new Car(...); a bare declaration creates no object.