Java Starter · free course

Free course · Lesson 4 of 5

Object-Oriented Programming Basics

In this lesson you will

  • Write a class with private fields, a constructor and methods
  • Create several objects from one class

Java is built around classes and objects. A class is a blueprint describing fields (data) and methods (behavior); an object is a concrete instance built from that blueprint with the new keyword.

  • private fields protect data; public methods expose safe access — this is encapsulation.
  • A constructor runs automatically when you create an object with new.
  • Getters and setters (getBrand(), setBrand()) are the conventional way to read/update private fields.
  • IntelliJ IDEA can auto-generate constructors, getters, and setters — use it to save time.

💡 Practical Skill: Modeling a real-world thing (a student, an order, a vehicle) as a class is the single most important habit for writing maintainable Java.

Worked exampleCar Class
Java
public class Car {    private String brand;   // property, hidden from outside     public Car(String brand) {   // constructor        this.brand = brand;    }     public String getBrand() {        return brand;    }     public void honk() {         // method        System.out.println(brand + " says: Beep beep!");    }     public static void main(String[] args) {        Car myCar = new Car("Toyota");        System.out.println(myCar.getBrand());  // Toyota        myCar.honk();                          // Toyota says: Beep beep!    }}

Practice exercise

Write a Book class with title, author and available fields, and methods to lend and return the book; create two books and test both methods.

Key takeaways

  • A class is a blueprint; objects are built from it with new
  • Private fields protect data; methods enforce the rules
  • Each object keeps its own copy of the fields
In the full program Module 4: Classes and Objects in Java See how this topic is taught in depth, with practical activities and an assessment.

Free self-study lesson — no certificate. For the full instructor-led program, see registration options.