New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› Java› Classes and Objects

Classes and Objects

💻 Java

Model real things with fields, constructors and methods.

Lesson 6 of 9 Tutorial
0 of 9 completed

Java is object-oriented. A class describes the data and behaviour of one kind of thing; an object is one instance.

By the end of this lesson you can:
  • Create a class with fields and methods.
  • Write constructors.
  • Use encapsulation with getters.

A Class

Fields hold state. Methods define behaviour. Keep fields private so the class controls how they change.

public class Student {
    private String name;
    private int score;

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void addScore(int points) {
        if (points > 0) {
            score += points;
        }
    }

    public String grade() {
        if (score >= 70) return "A";
        if (score >= 60) return "B";
        if (score >= 50) return "C";
        return "F";
    }
}

Using Objects

Create objects with new and call methods with dot notation.

Student student = new Student("Amina", 64);
student.addScore(8);
System.out.println(student.getName() + ": " + student.grade());

Encapsulation

Private fields plus public methods let you change the internals later without breaking callers. Validation belongs inside the setter or constructor.

Tip A class should have one reason to change. Split classes that handle both data and unrelated work such as sending email.

Practice

  1. Create a Book class with title, author and price.
  2. Add a method that returns a formatted description.
  3. Prevent a negative price in the constructor.

Hands-on exercise

Create a BankAccount class with:
1. A private balance.
2. A constructor that accepts an opening balance.
3. deposit and withdraw methods.
4. withdraw refuses to go below zero.
5. A getBalance method.

Recap

Classes group state and behaviour. Private fields with validated methods keep objects valid.

Example

public class Student {
    private String name;
    private int score;

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void addScore(int points) {
        if (points > 0) {
            score += points;
        }
    }

    public String grade() {
        if (score >= 70) return "A";
        if (score >= 60) return "B";
        if (score >= 50) return "C";
        return "F";
    }
}

Exercises

Exercise 1. Bank account Medium
Create a BankAccount class with:
1. A private balance.
2. A constructor that accepts an opening balance.
3. deposit and withdraw methods.
4. withdraw refuses to go below zero.
5. A getBalance method.
Java exercise-1.java
Expected Output
1500.0 — the oversized withdrawal is refused.

Lesson Nav

Lesson sections