Sitemap
Harshh🎀

Harshh🎀

Java program on Encapsulation

2 min readNov 21, 2024

Write a Java program to create a BankAccount class with private instance variables such as Account Number and balance.
Provide public getter and setter methods to access and modify these variables.

public class BankAccount {
// Private instance variables
private String accountNumber;
private double balance;

// Constructor to initialize the bank account
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}

// Getter method for accountNumber
public String getAccountNumber() {
return accountNumber;
}

// Setter method for accountNumber
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}

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

// Setter method for balance
public void setBalance(double balance) {
this.balance = balance;
}

// Main method to test the BankAccount class
public static void main(String[] args) {
// Create a new bank account
BankAccount account = new BankAccount("123456789", 1000.00);

// Display account information
System.out.println("Account Number: " + account.getAccountNumber());
System.out.println("Balance: " + account.getBalance());

// Update the account number and balance
account.setAccountNumber("987654321");
account.setBalance(1500.00);

// Display updated account information
System.out.println("Updated Account Number: " + account.getAccountNumber());
System.out.println("Updated Balance: " + account.getBalance());
}
}

output:

Get Harshh🎀’s stories in your inbox

Join Medium for free to get updates from this writer.

Account Number: 123456789
Balance: 1000.0
Updated Account Number: 987654321
Updated Balance: 1500.0

02)Basic Encapsulation Create a class named Student. Add private fields: name (String), age (int), and grade (char). Provide public getter and setter methods for each of these fields. Write a main method that creates an instance of Student, sets the fields using the setter methods, and then retrieves and prints the values using the getter methods.

public class Student {
// Private fields
private String name;
private int age;
private char grade;

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

public void setName(String name) {
this.name = name;
}

// Getter and Setter for age
public int getAge() {
return age;
}

public void setAge(int age) {
if (age >= 0) { // Ensuring age is non-negative
this.age = age;
} else {
System.out.println("Age cannot be negative.");
}
}

// Getter and Setter for grade
public char getGrade() {
return grade;
}

public void setGrade(char grade) {
this.grade = grade;
}

// Main method
public static void main(String[] args) {
// Create an instance of Student
Student student = new Student();

// Set fields using setter methods
student.setName("Alice");
student.setAge(20);
student.setGrade('A');

// Retrieve and print the values using getter methods
System.out.println("Student Name: " + student.getName());
System.out.println("Student Age: " + student.getAge());
System.out.println("Student Grade: " + student.getGrade());
}
}

Output:

Student Name: Alice
Student Age: 20
Student Grade: A

--

--