Code

// Poor encapsulation (public fields)class BadPerson {    public String name;  // Direct access - not recommended    public int age;     // Can be set to any value}// Good encapsulation (private fields with getters/setters)class Person {    private String name;    private int age;        // Getter for name    public String getName() {        return name;    }        // Setter for name with validation    public void setName(String name) {        if (name == null || name.trim().isEmpty()) {            throw new IllegalArgumentException("Name cannot be empty");        }        this.name = name;    }        // Getter for age    public int getAge() {        return age;    }        // Setter for age with validation    public void setAge(int age) {        if (age < 0 || age > 150) {            throw new IllegalArgumentException("Age must be between 0 and 150");        }        this.age = age;    }}// UsagePerson person = new Person();person.setName("Alice");person.setAge(30);String name = person.getName();int age = person.getAge();// Immutable class (read-only)class ImmutablePerson {    private final String name;    private final int age;        public ImmutablePerson(String name, int age) {        if (name == null || name.trim().isEmpty()) {            throw new IllegalArgumentException("Name cannot be empty");        }        if (age < 0 || age > 150) {            throw new IllegalArgumentException("Age must be between 0 and 150");        }        this.name = name;        this.age = age;    }        public String getName() {        return name;    }        public int getAge() {        return age;    }        // No setters - object is immutable}// Read-only accessclass BankAccount {    private double balance;        public BankAccount(double initialBalance) {        this.balance = initialBalance;    }        // Read-only access    public double getBalance() {        return balance;    }        // Controlled modification    public void deposit(double amount) {        if (amount <= 0) {            throw new IllegalArgumentException("Amount must be positive");        }        balance += amount;    }        public void withdraw(double amount) {        if (amount <= 0) {            throw new IllegalArgumentException("Amount must be positive");        }        if (amount > balance) {            throw new IllegalArgumentException("Insufficient funds");        }        balance -= amount;    }}

Comments

No comments yet. Be the first!