root@coding-prodigies:~# โ–Š
// lesson 3 of 12 ยท 18 min

Classes and encapsulation

โš‘ Report an issue with this lesson
public class Course {
    private String title;
    private double price;

    public Course(String title, double price) {
        this.title = title;
        this.price = price;
    }

    public String describe() {
        return title + " -- $" + price;
    }
}

private fields with public methods is the standard Java encapsulation pattern -- callers interact through describe(), never touching title or price directly.

Try it yourself

Exercise: Add a describe() method to Course using its private fields.
Expected output: open-ended โ€” there's no single correct output here, just get your code running without errors.
java
Output

      
    

Run your code and get it working before marking this lesson complete.

// that was the last free lesson

9 more lessons โ€” including Capstone project: a course catalog service โ€” plus a certificate are waiting.

Unlock the full course โ€” $149.99