Classes and encapsulation
โ Report an issue with this lessonpublic 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.
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