🔓Encapsulation
class Person {
// **private** fields are hidden from outside access
private String name;
private int age;
// **public** methods allow access to the private fields
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
// A public method that prints the person's name and age
public void sayHello() {
System.out.println("Hello, my name is " + this.name + " and I am " + this.age + " years old.");
}
}🚪 Public, Private, Protected
🛡 Defining the Interface
💡 Rules for Encapsulation in Java 👨💻
🔐 Key Notes
Last updated