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

ISC Practicals and Theory - Account to Savings/Current

Updated 21 Oct 2026

A bank has different kinds of accounts that share common details (name, account number, balance) but follow different rules. Inheritance lets us write the common part once in a base class Account and the special rules in the derived classes Savings and Current. This version is the classic form: every class has a default constructor, and the classes read and print their data with accept() and display(), calling the base class versions with super.

Definition

Inheritance is the process by which a class (the derived class or subclass) acquires the data members and methods of another class (the base class or superclass). The keyword extends is used, and the derived class can add its own members. super.accept() and super.display() call the base class version of a method that the derived class has redefined with the same name.

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.

  • Account: holds name, accNo and balance. Opening balance must not be negative, and the deposit and withdrawal amounts must be greater than 0, otherwise the program prints INVALID INPUT.
  • Savings: one year's simple interest is calculated on the balance as $\text{interest} = \dfrac{\text{balance} \times \text{rate}}{100}$ and added once, after the deposit and the withdrawal. The rate is typed by the user and must be greater than 0.
  • Savings withdrawal: 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. A withdrawal is rejected if the amount is more than the balance.
  • Current service charge: 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 balance is not charged. The charge is deducted even if it makes the balance negative.

For example, a Savings account with balance 5000, deposit 1000, withdrawal 2000 and rate 4: the balance becomes $5000 + 1000 - 2000 = 4000$, interest is $4000 \times 4 \div 100 = 160$, and the final balance is 4160.

Algorithm

  1. main (in the driver class Test) reads the account type. If it is not S or C (capital or small), it prints INVALID INPUT and stops.
  2. For S, main creates an object ob of Savings; for C, an object ob of Current. The default constructors set the data members to 0 or empty.
  3. main calls ob.accept():
    • Savings.accept() or Current.accept() first calls super.accept(), which reads name, accNo, balance, dep and wd and returns false if the balance is negative or an amount is not positive;
    • then it reads the rate (Savings) or the minimum balance and service charge (Current) and checks them.
  4. If accept() returned false, main prints INVALID INPUT.
  5. Otherwise main calls ob.deposit() (the base class method) to add dep to balance.
  6. main calls ob.withdraw(), defined in the derived class:
    • if wd > balance it prints the rejection message and the balance is unchanged;
    • otherwise wd is subtracted from balance;
    • in Current, if the balance is now less than minBal, charge is subtracted as well.
  7. For a Savings account main calls ob.addInterest().
  8. main calls ob.display(), which calls super.display() to print the common details and then prints the details of its own type.

Program

The base class Account has the common data and the methods accept(), deposit() and display(). Savings and Current extend it and redefine accept() and display() with calls to super.

import java.util.Scanner;

/**
 * Base class Account: holds the details common to every bank account.
 */
class Account
{
    static Scanner sc = new Scanner(System.in);   // one Scanner shared by all classes (avoids losing typed input)
    String name;       // name of the account holder
    long accNo;        // account number
    double balance;    // current balance in rupees
    double dep;        // amount to be deposited
    double wd;         // amount to be withdrawn

    /** Default constructor: sets every data member to a neutral value. */
    Account()
    {
        name = "";
        accNo = 0;
        balance = 0;
        dep = 0;
        wd = 0;
    }

    /**
     * Reads the common details from the user.
     * @return true if the input is valid, false otherwise
     */
    boolean accept()
    {
        System.out.print("Enter name: ");
        name = sc.nextLine();
        System.out.print("Enter account number: ");
        accNo = sc.nextLong();
        System.out.print("Enter opening balance: ");
        balance = sc.nextDouble();
        System.out.print("Enter deposit amount: ");
        dep = sc.nextDouble();
        System.out.print("Enter withdrawal amount: ");
        wd = sc.nextDouble();
        // balance may be 0 but not negative; amounts must be positive
        return balance >= 0 && dep > 0 && wd > 0;
    }

    /** Adds the deposit amount dep to the balance. */
    void deposit()
    {
        balance = balance + dep;
        System.out.println("Deposit of " + dep + " done. Balance: " + balance);
    }

    /** Prints the details common to every account. */
    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
    double interest;   // interest for one year

    /** Default constructor. */
    Savings()
    {
        rate = 0;
        interest = 0;
    }

    /**
     * Reads the common details (through super.accept()) and the rate of interest.
     * @return true if the input is valid, false otherwise
     */
    boolean accept()
    {
        if (!super.accept())
        {
            return false;
        }
        System.out.print("Enter rate of interest (%): ");
        rate = sc.nextDouble();
        return rate > 0;
    }

    /** Withdraws wd; rejected if it would take the balance below 0. */
    void withdraw()
    {
        if (wd > balance)
        {
            System.out.println("Withdrawal rejected: insufficient balance.");
        }
        else
        {
            balance = balance - wd;
            System.out.println("Withdrawal of " + wd + " done. Balance: " + balance);
        }
    }

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

    /** Prints the common details (super.display()) and 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

    /** Default constructor. */
    Current()
    {
        minBal = 0;
        charge = 0;
    }

    /**
     * Reads the common details (through super.accept()), the minimum balance and the service charge.
     * @return true if the input is valid, false otherwise
     */
    boolean accept()
    {
        if (!super.accept())
        {
            return false;
        }
        System.out.print("Enter minimum balance: ");
        minBal = sc.nextDouble();
        System.out.print("Enter service charge: ");
        charge = sc.nextDouble();
        return minBal >= 0 && charge >= 0;
    }

    /** Withdraws wd; rejected if it exceeds the balance; service charge if balance then falls below minBal. */
    void withdraw()
    {
        if (wd > balance)
        {
            System.out.println("Withdrawal rejected: insufficient balance.");
        }
        else
        {
            balance = balance - wd;
            System.out.println("Withdrawal of " + wd + " done. Balance: " + balance);
            if (balance < minBal)
            {
                balance = balance - charge;
                System.out.println("Service charge of " + charge + " deducted. Balance: " + balance);
            }
        }
    }

    /** Prints the common details (super.display()) and 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 account type, creates one Savings or Current object and runs it.
 */
public class Test
{
    /** Entry point: creates the object for the type typed by the user and calls its methods. */
    public static void main(String args[])
    {
        System.out.print("Enter account type (S for Savings, C for Current): ");
        String t = Account.sc.next();   // type typed by the user
        Account.sc.nextLine();          // discard the rest of the line before reading the name
        char type = Character.toUpperCase(t.charAt(0));
        if (type == 'S')
        {
            Savings ob = new Savings();
            if (ob.accept())
            {
                ob.deposit();
                ob.withdraw();
                ob.addInterest();
                ob.display();
            }
            else
            {
                System.out.println("INVALID INPUT");
            }
        }
        else if (type == 'C')
        {
            Current ob = new Current();
            if (ob.accept())
            {
                ob.deposit();
                ob.withdraw();
                ob.display();
            }
            else
            {
                System.out.println("INVALID INPUT");
            }
        }
        else
        {
            System.out.println("INVALID INPUT");
        }
    }
}

Variable description table

VariableData TypePurpose
scScannerStatic data member of Account: reads all the typed input; shared so that no typed input is lost
nameStringData member of Account: name of the account holder
accNolongData member of Account: account number
balancedoubleData member of Account: current balance
depdoubleData member of Account: amount to be deposited
wddoubleData member of Account: amount to be withdrawn
ratedoubleData member of Savings: rate of interest in per cent per year
interestdoubleData member of Savings: interest for one year
minBaldoubleData member of Current: minimum balance to be maintained
chargedoubleData member of Current: service charge deducted when the balance is below minBal
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
obSavings or CurrentLocal object in main (one in the S branch, one in the C branch)
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: Sita Das
Enter account number: 5005
Enter opening balance: 5000
Enter deposit amount: 1000
Enter withdrawal amount: 5000
Enter minimum balance: 1000
Enter service charge: 50
Deposit of 1000.0 done. Balance: 6000.0
Withdrawal of 5000.0 done. Balance: 1000.0
Name: Sita Das
Account number: 5005
Balance: 1000.0
Account type: Current
Minimum balance: 1000.0
Service charge: 50.0

Sample 5

Enter account type (S for Savings, C for Current): X
INVALID INPUT

One value per line

Dry run

Take the first sample: type S, name Asha Rao, accNo 1001, balance 5000, dep 1000, wd 2000, rate 4.

StepStatementbalanceOther values
1type = 'S', ob = new Savings()0rate = 0, interest = 0
2ob.accept() reads the details5000dep = 1000, wd = 2000, rate = 4; returns true
3ob.deposit(): balance = 5000 + 10006000
4ob.withdraw(): wd > balance is false, balance = 6000 - 20004000
5ob.addInterest(): interest = 4000 * 4 / 1004000interest = 160
6balance = 4000 + 1604160
7ob.display() prints the details4160

The final balance is 4160.0, as in the first 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."