Simple coding example for Inheritance

Published: (March 15, 2026 at 06:09 PM EDT)
2 min read
Source: Dev.to

Source: Dev.to

Simple coding example for Bank system

package bank.task;

public class BankAccount {

    int accountNumber;
    double balance;

    public void deposit(double depositAmount, int accNo, String AccType) {
        double total = balance + depositAmount;
        System.out.println("An amount of Rs." + depositAmount + "  has been deposited succesfully ");
        System.out.println("The total amount in your " + AccType + "  Account of   Account number" + accNo + " is " + total);
    }

    protected void withdraw(double withdrawAmount, int accNo, String AccType) {
        double total = balance - withdrawAmount;
        System.out.println("An amount of Rs." + withdrawAmount + " has been withdrawn succesfully ");
        System.out.println("The total amount in your " + AccType + "  Account of  your Account number" + accNo + " is " + total);
    }
}

class SavingAccount extends BankAccount {
    double interestRate;

    public void displayInterest(double rate) {
        System.out.println("The interest rate is" + rate);
    }
}

class CurrentAccount extends BankAccount {
    int limit = 2000;

    public void payPenalty(float balance) {
        if (balance < limit)
            System.out.println("Your balance is less than Rs. " + limit + " . Please pay a penalty of Rs.500");
        else
            System.out.println("Your balance is above than Rs. " + limit + " .NO penalty");
    }
}

class User {
    public static void main(String args[]) {
        SavingAccount sa = new SavingAccount();
        CurrentAccount ca = new CurrentAccount();

        sa.balance = 10000.00;
        sa.deposit(2000.00, 1234567, "Savings");
        sa.withdraw(2345.00, 1234567, "Savings");
        sa.displayInterest(6.7F);

        ca.balance = 1900;
        ca.payPenalty((float) ca.balance);
    }
}
0 views
Back to Blog

Related posts

Read more »