GETTERS AND SETTERS

I'm having trouble understanding how getters and setters work. can anyone help explain them to me in like a summarized format. Thanks!
3 Replies
JavaBot
JavaBot3mo ago
This post has been reserved for your question.
Hey @Abdullah! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Logan17
Logan173mo ago
It's literally what their names say. Getters and setters are methods that allow controlled access to a class's attributes. A getter retrieves the value of an attribute, while a setter modifies it. For example, if you have a private attribute that you don't want to expose directly to other classes, you can create a public getter that allows access to that value. This way, other classes can retrieve the value without directly accessing the attribute. Similarly, if you need to change the value of that private attribute, you can use a setter, which allows modification without exposing the attribute directly. This approach helps maintain encapsulation and data integrity within your class. Ex.
public class Employee {
private String name;
private double salary;

public Employee(String name, double salary) {
this.name = name;
setSalary(salary); // Using the setter for validation
}

// Getter for name
public String getName() {
return name;
}

// Getter for salary
public double getSalary() {
return salary;
}

// Setter for salary with validation
public void setSalary(double salary) {
if (salary >= 0) {
this.salary = salary;
}
}

// Private method to calculate tax (example)
private double calculateTax() {
return salary * 0.2; // 20% tax
}

// Public method to get tax amount
public double getTax() {
return calculateTax();
}
}

// Example usage
public class Main {
public static void main(String[] args) {
Employee emp = new Employee("Alice", 5000.0);

System.out.println("Name: " + emp.getName());
System.out.println("Salary: " + emp.getSalary());
System.out.println("Tax: " + emp.getTax());

emp.setSalary(6000.0); // Updating salary
System.out.println("Updated Salary: " + emp.getSalary());
System.out.println("Updated Tax: " + emp.getTax());
}
}
public class Employee {
private String name;
private double salary;

public Employee(String name, double salary) {
this.name = name;
setSalary(salary); // Using the setter for validation
}

// Getter for name
public String getName() {
return name;
}

// Getter for salary
public double getSalary() {
return salary;
}

// Setter for salary with validation
public void setSalary(double salary) {
if (salary >= 0) {
this.salary = salary;
}
}

// Private method to calculate tax (example)
private double calculateTax() {
return salary * 0.2; // 20% tax
}

// Public method to get tax amount
public double getTax() {
return calculateTax();
}
}

// Example usage
public class Main {
public static void main(String[] args) {
Employee emp = new Employee("Alice", 5000.0);

System.out.println("Name: " + emp.getName());
System.out.println("Salary: " + emp.getSalary());
System.out.println("Tax: " + emp.getTax());

emp.setSalary(6000.0); // Updating salary
System.out.println("Updated Salary: " + emp.getSalary());
System.out.println("Updated Tax: " + emp.getTax());
}
}
Ex.2
public class BankAccount {
private double balance;

public BankAccount(double initialBalance) {
this.balance = initialBalance;
}

// Getter for balance
public double getBalance() {
return balance;
}

// Setter for balance (with validation)
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}

public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
}

// Example usage
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(100.0);

// Depositing money
account.deposit(50.0);
System.out.println("Balance after deposit: " + account.getBalance());

// Withdrawing money
account.withdraw(30.0);
System.out.println("Balance after withdrawal: " + account.getBalance());
}
}
public class BankAccount {
private double balance;

public BankAccount(double initialBalance) {
this.balance = initialBalance;
}

// Getter for balance
public double getBalance() {
return balance;
}

// Setter for balance (with validation)
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}

public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
}

// Example usage
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(100.0);

// Depositing money
account.deposit(50.0);
System.out.println("Balance after deposit: " + account.getBalance());

// Withdrawing money
account.withdraw(30.0);
System.out.println("Balance after withdrawal: " + account.getBalance());
}
}
JavaBot
JavaBot3mo ago
Post Closed
This post has been closed by <@470002259841777686>.
Want results from more Discord servers?
Add your server