Getters and setters
Getters and setters — are methods that provide controlled access to private fields.
public class Car {
private String color;
private int speed;
// Getter for color
public String getColor() {
return color;
}
// Setter for color
public void setColor(String color) {
this.color = color;
}
} This is essential for encapsulation, allowing you to protect and control changes to data.
Semantic portal