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,accNoandbalance. Opening balance must not be negative, and the deposit and withdrawal amounts must be greater than 0, otherwise the program printsINVALID 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
main(in the driver classTest) reads the account type. If it is notSorC(capital or small), it printsINVALID INPUTand stops.- For
S,maincreates an objectobofSavings; forC, an objectobofCurrent. The default constructors set the data members to 0 or empty. maincallsob.accept():Savings.accept()orCurrent.accept()first callssuper.accept(), which readsname,accNo,balance,depandwdand returnsfalseif 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.
- If
accept()returnedfalse,mainprintsINVALID INPUT. - Otherwise
maincallsob.deposit()(the base class method) to adddeptobalance. maincallsob.withdraw(), defined in the derived class:- if
wd > balanceit prints the rejection message and the balance is unchanged; - otherwise
wdis subtracted frombalance; - in
Current, if the balance is now less thanminBal,chargeis subtracted as well.
- if
- For a Savings account
maincallsob.addInterest(). maincallsob.display(), which callssuper.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
| Variable | Data Type | Purpose |
|---|---|---|
sc | Scanner | Static data member of Account: reads all the typed input; shared so that no typed input is lost |
name | String | Data member of Account: name of the account holder |
accNo | long | Data member of Account: account number |
balance | double | Data member of Account: current balance |
dep | double | Data member of Account: amount to be deposited |
wd | double | Data member of Account: amount to be withdrawn |
rate | double | Data member of Savings: rate of interest in per cent per year |
interest | double | Data member of Savings: interest for one year |
minBal | double | Data member of Current: minimum balance to be maintained |
charge | double | Data member of Current: service charge deducted when the balance is below minBal |
t | String | Local variable in main: the account type text typed by the user |
type | char | Local variable in main: first letter of t in capitals, S or C |
ob | Savings or Current | Local object in main (one in the S branch, one in the C branch) |
args | String[] | 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.0Sample 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.0Sample 5
Enter account type (S for Savings, C for Current): X
INVALID INPUTDry run
Take the first sample: type S, name Asha Rao, accNo 1001, balance 5000, dep 1000, wd 2000, rate 4.
| Step | Statement | balance | Other values |
|---|---|---|---|
| 1 | type = 'S', ob = new Savings() | 0 | rate = 0, interest = 0 |
| 2 | ob.accept() reads the details | 5000 | dep = 1000, wd = 2000, rate = 4; returns true |
| 3 | ob.deposit(): balance = 5000 + 1000 | 6000 | |
| 4 | ob.withdraw(): wd > balance is false, balance = 6000 - 2000 | 4000 | |
| 5 | ob.addInterest(): interest = 4000 * 4 / 100 | 4000 | interest = 160 |
| 6 | balance = 4000 + 160 | 4160 | |
| 7 | ob.display() prints the details | 4160 |
The final balance is 4160.0, as in the first sample run.
Variations
- Overdraft "Allow a Current account to be overdrawn up to a given limit and charge a fee when it is used."
- Compound interest "Add interest for n years on a Savings account, adding the interest to the balance every year."
- Abstract class "Make
Accountan abstract class with an abstract methodwithdraw()and define it inSavingsandCurrent."