Explain this program clearly | Core Java Forum
H
Harika Posted on 08/07/2023

this is example program on abstract class using banking I couldnt understand this programm. please explain me clearly.

// BankAccount.java
// Abstract class Shape

abstract class BankAccount {
    private String accountNumber;
    private double balance;

    public BankAccount(String accountNumber, double balance) {
        this.accountNumber = accountNumber;
        this.balance = balance;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public double getBalance() {
        return balance;
    }

    protected void setBalance(double balance) {
        this.balance = balance;
    }

    public abstract void deposit(double amount);

    public abstract void withdraw(double amount);
}

// SavingsAccount.java
// Subclass SavingsAccount

class SavingsAccount extends BankAccount {
    public SavingsAccount(String accountNumber, double balance) {
        super(accountNumber, balance);
    }

    @Override
    public void deposit(double amount) {
        setBalance(getBalance() + amount);
        System.out.println("Deposit of $" + amount + " successful. Current balance: $" + getBalance());
    }

    @Override
    public void withdraw(double amount) {
        if (getBalance() >= amount) {
            setBalance(getBalance() - amount);
            System.out.println("Withdrawal of $" + amount + " successful. Current balance: $" + getBalance());
        } else {
            System.out.println("Insufficient funds. Withdrawal failed.");
        }
    }
}
// CurrentAccount.java
// Subclass CurrentAccount

class CurrentAccount extends BankAccount {
    public CurrentAccount(String accountNumber, double balance) {
        super(accountNumber, balance);
    }

    @Override
    public void deposit(double amount) {
        setBalance(getBalance() + amount);
        System.out.println("Deposit of $" + amount + " successful. Current balance: $" + getBalance());
    }

    @Override
    public void withdraw(double amount) {
        if (getBalance() >= amount) {
            setBalance(getBalance() - amount);
            System.out.println("Withdrawal of $" + amount + " successful. Current balance: $" + getBalance());
        } else {
            System.out.println("Insufficient funds. Withdrawal failed.");
        }
    }
}
// Main.java
// Subclass Main

public class Main {
    public static void main(String[] args) {
		double ibal,damt,wamt;
        ibal = 1000.00;
        SavingsAccount savingsAccount = new SavingsAccount("SA001", ibal);
		System.out.println("Savings A/c: Initial Balace: $"+ibal);
		damt = 500.00;
        savingsAccount.deposit(damt);
		wamt = 250.00;
        savingsAccount.withdraw(wamt);
		wamt = 1600.00;
		System.out.println("\nTry to withdraw: $"+wamt);
        savingsAccount.withdraw(wamt);

        System.out.println();
        ibal = 5000.00;
        CurrentAccount currentAccount = new CurrentAccount("CA001", ibal);
		System.out.println("Current A/c: Initial Balace: $"+ibal);
		damt = 2500.00;
        currentAccount.deposit(1000.0);
		wamt = 1250.00;
        currentAccount.withdraw(3000.0);
		wamt = 6000.00;
		System.out.println("\nTry to withdraw: $"+wamt);
        savingsAccount.withdraw(wamt);		
    }
}

Y
Yogesh Chawla Replied on 11/07/2023

I have explained functionally how abstract class and child classes are actually working. See the comments.

If you want to understand logically how withdraw and deposit works, just focus on thier individual implementation, you will come to know. If you could not get it, just message me.

package com.learning.kafkagettingstarted.chapter5;

//BankAccount.java
//Abstract class Shape
//This is a good example for creating abstract class because both Saving and Current Account
//below are actually kind of Bank Accounts
abstract class BankAccount { //this is a abstract class where declaration and
    //definition both can exist
    private String accountNumber;
    private double balance;

    //constructor
    public BankAccount(String accountNumber, double balance) {
        this.accountNumber = accountNumber;
        this.balance = balance;
    }

    //getter and setter methods for non static instance variable
    public String getAccountNumber() {
        return accountNumber;
    }

    public double getBalance() {
        return balance;
    }

    protected void setBalance(double balance) {
        this.balance = balance;
    }

    //unique functionality which has to be defined by child classes
    public abstract void deposit(double amount);

    //unique functionality which has to be defined by child classes
    public abstract void withdraw(double amount);
}

//SavingsAccount.java
//Subclass SavingsAccount
//Child class extending abstract class to define unique functionality
//declared in abstract class
class SavingsAccount extends BankAccount {
    //Constructor
    public SavingsAccount(String accountNumber, double balance) {
        super(accountNumber, balance);
    }

    //Definition of deposit method in Savings Account
    @Override
    public void deposit(double amount) {
        setBalance(getBalance() + amount);
        System.out.println("Deposit of $" + amount + " successful. Current balance: $" + getBalance());
    }

    //Definition of withdraw method in Savings Account
    @Override
    public void withdraw(double amount) {
        if (getBalance() >= amount) {
            setBalance(getBalance() - amount);
            System.out.println("Withdrawal of $" + amount + " successful. Current balance: $" + getBalance());
        } else {
            System.out.println("Insufficient funds. Withdrawal failed.");
        }
    }
}
//CurrentAccount.java
//Subclass CurrentAccount
//Another Child class extending abstract class to define unique functionality
//declared in abstract class
class CurrentAccount extends BankAccount {
    public CurrentAccount(String accountNumber, double balance) {
        super(accountNumber, balance);
    }

    //Definition of deposit method in Current Account
    @Override
    public void deposit(double amount) {
        setBalance(getBalance() + amount);
        System.out.println("Deposit of $" + amount + " successful. Current balance: $" + getBalance());
    }

    //Definition of withdraw method in Current Account
    @Override
    public void withdraw(double amount) {
        if (getBalance() >= amount) {
            setBalance(getBalance() - amount);
            System.out.println("Withdrawal of $" + amount + " successful. Current balance: $" + getBalance());
        } else {
            System.out.println("Insufficient funds. Withdrawal failed.");
        }
    }
}
//Main.java
//Subclass Main

public class Main {
    public static void main(String[] args) {
        double ibal,damt,wamt;
        ibal = 1000.00;
        //Saving Account Method Execution
        SavingsAccount savingsAccount = new SavingsAccount("SA001", ibal);
        System.out.println("Savings A/c: Initial Balace: $"+ibal);
        damt = 500.00;
        savingsAccount.deposit(damt);
        wamt = 250.00;
        savingsAccount.withdraw(wamt);
        wamt = 1600.00;
        System.out.println("\nTry to withdraw: $"+wamt);
        savingsAccount.withdraw(wamt);

        System.out.println();
        ibal = 5000.00;
        //Current Account Method Execution
        CurrentAccount currentAccount = new CurrentAccount("CA001", ibal);
        System.out.println("Current A/c: Initial Balace: $"+ibal);
        damt = 2500.00;
        currentAccount.deposit(1000.0);
        wamt = 1250.00;
        currentAccount.withdraw(3000.0);
        wamt = 6000.00;
        System.out.println("\nTry to withdraw: $"+wamt);
        savingsAccount.withdraw(wamt);
    }
}

Output:

Savings A/c: Initial Balace: $1000.0
Deposit of $500.0 successful. Current balance: $1500.0
Withdrawal of $250.0 successful. Current balance: $1250.0

Try to withdraw: $1600.0
Insufficient funds. Withdrawal failed.

Current A/c: Initial Balace: $5000.0
Deposit of $1000.0 successful. Current balance: $6000.0
Withdrawal of $3000.0 successful. Current balance: $3000.0

Try to withdraw: $6000.0
Insufficient funds. Withdrawal failed.