ISC Practicals and Theory - Employee to Salary
Updated 21 Oct 2026
A pay slip starts with the basic pay of an employee and adds allowances and subtracts deductions to get the pay received. Using inheritance, the common employee details (name, code, basic pay) are kept in a base class Employee, and the derived class Salary adds the calculation of DA, HRA, PF, gross pay and net pay. This version is the classic form: default constructors and accept(), display() methods, with the derived class calling the base class methods through super.
Definition
Inheritance is the process by which a derived class (written with extends) acquires the data members and methods of a base class and adds its own. super.accept() and super.display() call the base class versions of methods that the derived class has also defined.
The percentages below are a sample set. An ISC question gives its own percentages, so use the question's figures in place of these.
- DA (dearness allowance) is 40% of basic pay.
- HRA (house rent allowance) is 20% of basic pay.
- PF (provident fund) is 12% of basic pay and is a deduction.
- $\text{Gross pay} = \text{basic pay} + DA + HRA$
- $\text{Net pay} = \text{gross pay} - PF$
- Basic pay must be greater than 0; otherwise the program prints
INVALID INPUT.
For example, with basic pay 1000: DA is 400, HRA is 200, PF is 120, gross pay is $1000 + 400 + 200 = 1600$ and net pay is $1600 - 120 = 1480$.
Algorithm
main(in the driver classPaySlip) creates an objectobof the derived classSalary. The default constructors set the data members to 0 or empty.maincallsob.accept().Salary.accept()callssuper.accept(), which readsname,empCodeandbasicPayand returnstrueonly ifbasicPay > 0.- If it returned
false,mainprintsINVALID INPUTand stops. - Otherwise
maincallsob.calculate(), which finds:da = basicPay * 40 / 100hra = basicPay * 20 / 100pf = basicPay * 12 / 100gross = basicPay + da + hranet = gross - pf
maincallsob.display().Salary.display()prints the heading, callssuper.display()to print the name, code and basic pay, and then prints DA, HRA, PF, gross pay and net pay.
Program
Employee holds the common data with accept() and display(). Salary extends it, redefines accept() and display() and adds calculate().
import java.util.Scanner;
/**
* Base class Employee: holds the details common to every employee.
*/
class Employee
{
Scanner sc = new Scanner(System.in); // reads the typed input (inherited by Salary)
String name; // name of the employee
int empCode; // employee code
double basicPay; // basic pay per month in rupees
/** Default constructor: sets every data member to a neutral value. */
Employee()
{
name = "";
empCode = 0;
basicPay = 0;
}
/**
* Reads the employee details from the user.
* @return true if basicPay is greater than 0, false otherwise
*/
boolean accept()
{
System.out.print("Enter name: ");
name = sc.nextLine();
System.out.print("Enter employee code: ");
empCode = sc.nextInt();
System.out.print("Enter basic pay: ");
basicPay = sc.nextDouble();
return basicPay > 0;
}
/** Prints the details common to every employee. */
void display()
{
System.out.println("Name: " + name);
System.out.println("Employee code: " + empCode);
System.out.println("Basic pay: " + basicPay);
}
}
/**
* Derived class Salary: an Employee whose allowances, deduction and net pay are calculated.
*/
class Salary extends Employee
{
double da; // dearness allowance
double hra; // house rent allowance
double pf; // provident fund deduction
double gross; // gross pay = basic pay + da + hra
double net; // net pay = gross pay - pf
/** Default constructor. */
Salary()
{
da = 0;
hra = 0;
pf = 0;
gross = 0;
net = 0;
}
/** Reads the details through the base class (super.accept()). */
boolean accept()
{
return super.accept();
}
/** Calculates DA (40% of basic), HRA (20% of basic), PF (12% of basic), gross pay and net pay. */
void calculate()
{
da = basicPay * 40 / 100;
hra = basicPay * 20 / 100;
pf = basicPay * 12 / 100;
gross = basicPay + da + hra;
net = gross - pf;
}
/** Prints the pay slip: the common details (super.display()) and then the pay details. */
void display()
{
System.out.println("----- PAY SLIP -----");
super.display();
System.out.println("DA (40%): " + da);
System.out.println("HRA (20%): " + hra);
System.out.println("PF (12%): " + pf);
System.out.println("Gross pay: " + gross);
System.out.println("Net pay: " + net);
}
}
/**
* Driver class: creates one Salary object and prints the pay slip.
*/
public class PaySlip
{
/** Entry point: reads the details, calculates the pay and displays the pay slip. */
public static void main(String args[])
{
Salary ob = new Salary(); // object of the derived class
if (ob.accept())
{
ob.calculate();
ob.display();
}
else
{
System.out.println("INVALID INPUT");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
sc | Scanner | Data member of Employee: reads the typed input; inherited by Salary |
name | String | Data member of Employee: name of the employee |
empCode | int | Data member of Employee: employee code |
basicPay | double | Data member of Employee: basic pay per month |
da | double | Data member of Salary: dearness allowance, 40% of basic pay |
hra | double | Data member of Salary: house rent allowance, 20% of basic pay |
pf | double | Data member of Salary: provident fund deduction, 12% of basic pay |
gross | double | Data member of Salary: gross pay, basic pay plus DA plus HRA |
net | double | Data member of Salary: net pay, gross pay minus PF |
ob | Salary | Object of the derived class created in main |
args | String[] | Command-line arguments of main (not used) |
Sample input and output
Sample 1
Enter name: Anita Sharma
Enter employee code: 101
Enter basic pay: 25000
----- PAY SLIP -----
Name: Anita Sharma
Employee code: 101
Basic pay: 25000.0
DA (40%): 10000.0
HRA (20%): 5000.0
PF (12%): 3000.0
Gross pay: 40000.0
Net pay: 37000.0Sample 2
Enter name: Rohan Mehta
Enter employee code: 102
Enter basic pay: 1000
----- PAY SLIP -----
Name: Rohan Mehta
Employee code: 102
Basic pay: 1000.0
DA (40%): 400.0
HRA (20%): 200.0
PF (12%): 120.0
Gross pay: 1600.0
Net pay: 1480.0Sample 3
Enter name: Kavya Nair
Enter employee code: 103
Enter basic pay: 0
INVALID INPUTSample 4
Enter name: Dev Patel
Enter employee code: 104
Enter basic pay: -5000
INVALID INPUTDry run
Take the second sample: name Rohan Mehta, empCode 102, basicPay 1000.
| Step | Statement | Value |
|---|---|---|
| 1 | ob.accept() reads the details and basicPay > 0 | basicPay = 1000, returns true |
| 2 | da = basicPay * 40 / 100 | da = 400 |
| 3 | hra = basicPay * 20 / 100 | hra = 200 |
| 4 | pf = basicPay * 12 / 100 | pf = 120 |
| 5 | gross = basicPay + da + hra = 1000 + 400 + 200 | gross = 1600 |
| 6 | net = gross - pf = 1600 - 120 | net = 1480 |
| 7 | ob.display() prints the pay slip | Net pay: 1480.0 |
The net pay is 1480.0, as in the second sample run.
Variations
- Different employee types "Write classes for Manager and Clerk derived from Employee, each with its own allowance percentages."
- Income tax "Deduct income tax from the gross pay at a rate that depends on the gross pay."
- Many employees "Read the details of n employees and print the pay slip of each."