Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Account to Savings/Current (OOP)

Updated 21 Oct 2026

A bank has different kinds of accounts that share common details (name, account number, balance) but follow different rules. This version uses parameterised constructors that pass the common details up with super(...), and method overriding: the derived classes Savings and Current redefine methods of the base class Account, and the driver calls them through a base class reference, so Java picks the right version at run time.

Definition

Inheritance lets a derived class (written with extends) acquire the members of a base class. A derived class constructor calls the base class constructor with super(...), which must be its first statement. Method overriding (redefinition) means a derived class defines a method with the same name and parameters as the base class. Polymorphism with dynamic binding means that when a base class reference Account ob holds a Savings or Current object, the call ob.display() runs the version belonging to the object, not to the reference type.

The rules below are a sample set for this program. An ISC question states its own rules, so when you answer one, use the question's rules in place of these.

  • Validation: opening balance must not be negative; deposit and withdrawal amounts must be greater than 0; otherwise the program prints INVALID INPUT.
  • Savings: one year's simple interest, $\text{interest} = \dfrac{\text{balance} \times \text{rate}}{100}$, is added once, after the deposit and the withdrawal. The rate is typed by the user and must be greater than 0.
  • Withdrawal (base rule): rejected if the amount is more than the balance, since the balance would go below 0. A withdrawal equal to the balance is allowed.
  • Current: the minimum balance and the service charge are typed by the user. The base rule above applies; after a successful withdrawal, if the balance is less than the minimum balance, the service charge is deducted once. A balance exactly equal to the minimum is not charged. The charge is deducted even if the balance becomes negative.

For example, a Current account with balance 5000, deposit 1000, withdrawal 5500, minimum balance 1000 and charge 50: the balance becomes $5000 + 1000 - 5500 = 500$, which is less than 1000, so 50 is deducted and the final balance is 450.

Algorithm

  1. main (in the driver class Test) reads the account type, name, account number, opening balance, deposit amount and withdrawal amount. If the type is not S or C, or the balance is negative or an amount is not positive, it prints INVALID INPUT and stops.
  2. For S, it reads the rate (must be above 0) and executes ob = new Savings(name, accNo, bal, rate). For C, it reads the minimum balance and the service charge (neither negative) and executes ob = new Current(name, accNo, bal, minBal, charge). ob is declared as Account.
  3. The derived constructor calls super(n, a, b), which sets name, accNo and balance, and then sets its own data member(s).
  4. main calls ob.deposit(dep), which adds the amount to balance.
  5. main calls ob.withdraw(wd):
    • for Savings this is the Account method: if amt > balance it prints the rejection message and returns false; otherwise it subtracts amt and returns true;
    • for Current the overridden method calls super.withdraw(amt) and, if it returned true and balance < minBal, subtracts charge.
  6. main calls ob.update(). In Account it does nothing; Savings overrides it to calculate and add the interest.
  7. main calls ob.display(). The overridden versions call super.display() and then print the details of their own type.

Program

Account has the common data and the methods deposit(), withdraw(), update() and display(). Savings overrides update() and display(); Current overrides withdraw() and display().

import java.util.Scanner;

/**
 * Base class Account: holds the details common to every bank account.
 */
class Account
{
    String name;       // name of the account holder
    long accNo;        // account number
    double balance;    // current balance in rupees

    /**
     * Parameterised constructor.
     * @param n name of the holder
     * @param a account number
     * @param b opening balance
     */
    Account(String n, long a, double b)
    {
        name = n;
        accNo = a;
        balance = b;
    }

    /**
     * Adds an amount to the balance.
     * @param amt amount to be deposited
     */
    void deposit(double amt)
    {
        balance = balance + amt;
        System.out.println("Deposit of " + amt + " done. Balance: " + balance);
    }

    /**
     * Withdraws an amount; rejected if it exceeds the balance (overridden in Current).
     * @param amt amount to be withdrawn
     * @return true if the withdrawal was done, false if it was rejected
     */
    boolean withdraw(double amt)
    {
        if (amt > balance)
        {
            System.out.println("Withdrawal rejected: insufficient balance.");
            return false;
        }
        balance = balance - amt;
        System.out.println("Withdrawal of " + amt + " done. Balance: " + balance);
        return true;
    }

    /** End-of-period processing; a plain Account has none (overridden in Savings). */
    void update()
    {
    }

    /** Prints the details common to every account (overridden in the derived classes). */
    void display()
    {
        System.out.println("Name: " + name);
        System.out.println("Account number: " + accNo);
        System.out.println("Balance: " + balance);
    }
}

/**
 * Derived class Savings: an Account that earns interest.
 */
class Savings extends Account
{
    double rate;       // rate of interest, per cent per year

    /**
     * Parameterised constructor; passes the common details to the base class with super(...).
     * @param n name of the holder
     * @param a account number
     * @param b opening balance
     * @param r rate of interest in per cent
     */
    Savings(String n, long a, double b, double r)
    {
        super(n, a, b);
        rate = r;
    }

    /** Overrides update(): adds one year's simple interest on the current balance. */
    void update()
    {
        double interest = balance * rate / 100;   // interest for one year
        balance = balance + interest;
        System.out.println("Interest of " + interest + " added. Balance: " + balance);
    }

    /** Overrides display(): common details plus the Savings details. */
    void display()
    {
        super.display();
        System.out.println("Account type: Savings");
        System.out.println("Rate of interest: " + rate + "%");
    }
}

/**
 * Derived class Current: an Account with a minimum balance and a service charge.
 */
class Current extends Account
{
    double minBal;    // minimum balance to be maintained
    double charge;    // service charge deducted when balance is below minBal

    /**
     * Parameterised constructor; passes the common details to the base class with super(...).
     * @param n name of the holder
     * @param a account number
     * @param b opening balance
     * @param m minimum balance
     * @param c service charge
     */
    Current(String n, long a, double b, double m, double c)
    {
        super(n, a, b);
        minBal = m;
        charge = c;
    }

    /**
     * Overrides withdraw(): uses the base class rule, then charges if balance falls below minBal.
     * @param amt amount to be withdrawn
     * @return true if the withdrawal was done, false if it was rejected
     */
    boolean withdraw(double amt)
    {
        boolean done = super.withdraw(amt);   // base class does the withdrawal or rejects it
        if (done && balance < minBal)
        {
            balance = balance - charge;
            System.out.println("Service charge of " + charge + " deducted. Balance: " + balance);
        }
        return done;
    }

    /** Overrides display(): common details plus the Current details. */
    void display()
    {
        super.display();
        System.out.println("Account type: Current");
        System.out.println("Minimum balance: " + minBal);
        System.out.println("Service charge: " + charge);
    }
}

/**
 * Driver class: reads the details, then uses an Account reference for a Savings or Current object.
 */
public class Test
{
    /** Entry point: reads the input, creates the object and calls the overridden methods through ob. */
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);   // reads the typed input
        System.out.print("Enter account type (S for Savings, C for Current): ");
        String t = sc.next();                  // type typed by the user
        sc.nextLine();                         // discard the rest of the line before reading the name
        char type = Character.toUpperCase(t.charAt(0));
        if (type != 'S' && type != 'C')
        {
            System.out.println("INVALID INPUT");
            return;
        }
        System.out.print("Enter name: ");
        String name = sc.nextLine();           // name of the holder
        System.out.print("Enter account number: ");
        long accNo = sc.nextLong();            // account number
        System.out.print("Enter opening balance: ");
        double bal = sc.nextDouble();          // opening balance
        System.out.print("Enter deposit amount: ");
        double dep = sc.nextDouble();          // amount to deposit
        System.out.print("Enter withdrawal amount: ");
        double wd = sc.nextDouble();           // amount to withdraw
        if (bal < 0 || dep <= 0 || wd <= 0)
        {
            System.out.println("INVALID INPUT");
            return;
        }
        Account ob;                            // base class reference, holds a derived class object
        if (type == 'S')
        {
            System.out.print("Enter rate of interest (%): ");
            double rate = sc.nextDouble();     // rate of interest
            if (rate <= 0)
            {
                System.out.println("INVALID INPUT");
                return;
            }
            ob = new Savings(name, accNo, bal, rate);
        }
        else
        {
            System.out.print("Enter minimum balance: ");
            double minBal = sc.nextDouble();   // minimum balance
            System.out.print("Enter service charge: ");
            double charge = sc.nextDouble();   // service charge
            if (minBal < 0 || charge < 0)
            {
                System.out.println("INVALID INPUT");
                return;
            }
            ob = new Current(name, accNo, bal, minBal, charge);
        }
        ob.deposit(dep);
        ob.withdraw(wd);
        ob.update();
        ob.display();
    }
}

Variable description table

VariableData TypePurpose
nameStringData member of Account: name of the account holder
accNolongData member of Account: account number
balancedoubleData member of Account: current balance
ratedoubleData member of Savings: rate of interest in per cent per year; also a local variable in main holding the typed rate
minBaldoubleData member of Current: minimum balance to be maintained; also a local variable in main holding the typed value
chargedoubleData member of Current: service charge; also a local variable in main holding the typed value
nStringConstructor parameter: name of the holder
alongConstructor parameter: account number
bdoubleConstructor parameter: opening balance
rdoubleParameter of the Savings constructor: rate of interest
mdoubleParameter of the Current constructor: minimum balance
cdoubleParameter of the Current constructor: service charge
amtdoubleParameter of deposit() and withdraw(): the amount
interestdoubleLocal variable in Savings.update(): interest for one year
donebooleanLocal variable in Current.withdraw(): whether the base class withdrawal succeeded
scScannerLocal variable in main: reads the typed input
tStringLocal variable in main: the account type text typed by the user
typecharLocal variable in main: first letter of t in capitals, S or C
accNo (in main)longLocal variable in main: account number typed by the user
baldoubleLocal variable in main: opening balance typed by the user
depdoubleLocal variable in main: deposit amount
wddoubleLocal variable in main: withdrawal amount
obAccountBase class reference in main, holding a Savings or a Current object
argsString[]Command-line arguments of main (not used)

Sample input and output

Sample 1

Enter account type (S for Savings, C for Current): S
Enter name: Asha Rao
Enter account number: 1001
Enter opening balance: 5000
Enter deposit amount: 1000
Enter withdrawal amount: 2000
Enter rate of interest (%): 4
Deposit of 1000.0 done. Balance: 6000.0
Withdrawal of 2000.0 done. Balance: 4000.0
Interest of 160.0 added. Balance: 4160.0
Name: Asha Rao
Account number: 1001
Balance: 4160.0
Account type: Savings
Rate of interest: 4.0%

Sample 2

Enter account type (S for Savings, C for Current): S
Enter name: Meena Iyer
Enter account number: 3003
Enter opening balance: 1000
Enter deposit amount: 500
Enter withdrawal amount: 2000
Enter rate of interest (%): 5
Deposit of 500.0 done. Balance: 1500.0
Withdrawal rejected: insufficient balance.
Interest of 75.0 added. Balance: 1575.0
Name: Meena Iyer
Account number: 3003
Balance: 1575.0
Account type: Savings
Rate of interest: 5.0%

Sample 3

Enter account type (S for Savings, C for Current): C
Enter name: Ravi Kumar
Enter account number: 2002
Enter opening balance: 5000
Enter deposit amount: 1000
Enter withdrawal amount: 5500
Enter minimum balance: 1000
Enter service charge: 50
Deposit of 1000.0 done. Balance: 6000.0
Withdrawal of 5500.0 done. Balance: 500.0
Service charge of 50.0 deducted. Balance: 450.0
Name: Ravi Kumar
Account number: 2002
Balance: 450.0
Account type: Current
Minimum balance: 1000.0
Service charge: 50.0

Sample 4

Enter account type (S for Savings, C for Current): C
Enter name: Om Jha
Enter account number: 6006
Enter opening balance: 2000
Enter deposit amount: 500
Enter withdrawal amount: 3000
Enter minimum balance: 1000
Enter service charge: 50
Deposit of 500.0 done. Balance: 2500.0
Withdrawal rejected: insufficient balance.
Name: Om Jha
Account number: 6006
Balance: 2500.0
Account type: Current
Minimum balance: 1000.0
Service charge: 50.0

Sample 5

Enter account type (S for Savings, C for Current): S
Enter name: Test
Enter account number: 7007
Enter opening balance: -100
Enter deposit amount: 500
Enter withdrawal amount: 200
INVALID INPUT

One value per line

Dry run

Take the third sample: type C, name Ravi Kumar, accNo 2002, bal 5000, dep 1000, wd 5500, minBal 1000, charge 50.

StepStatementbalanceOther values
1ob = new Current(...) calls super(n, a, b)5000minBal = 1000, charge = 50
2ob.deposit(1000): balance = 5000 + 10006000
3ob.withdraw(5500) runs Current.withdraw(), which calls super.withdraw(5500); 5500 > 6000 is false, balance = 6000 - 5500500done = true
4done && balance < minBal: 500 < 1000 is true, balance = 500 - 50450
5ob.update() runs Account.update(), which does nothing450
6ob.display() runs Current.display()450

The final balance is 450.0, as in the third sample run.

Variations

  1. Overdraft "Allow a Current account to be overdrawn up to a given limit and charge a fee when it is used."
  2. Compound interest "Add interest for n years on a Savings account, adding the interest to the balance every year."
  3. Abstract class "Make Account an abstract class with an abstract method withdraw() and define it in Savings and Current."