ISC Practicals and Theory - Employee to Salary (OOP)
Updated 21 Oct 2026
A pay slip starts with the basic pay of an employee, adds allowances and subtracts deductions to get the pay received. This version uses a parameterised constructor in the derived class Salary that passes the common details up with super(...), and method overriding: Salary redefines netPay() and display() of the base class Employee, and the driver calls display() through an Employee reference that holds a Salary object.
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(...) as its first statement. Method overriding means the derived class defines a method with the same name and parameters as the base class. With dynamic binding, ob.display() runs the version belonging to the object that ob holds, even when ob is declared as the base class Employee.
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 25000: DA is 10000, HRA is 5000, PF is 3000, gross pay is 40000 and net pay is 37000.
Algorithm
main(in the driver classPaySlip) readsname,codeandbasic.- If
basicis not greater than 0, it printsINVALID INPUTand stops. - Otherwise it executes
Employee ob = new Salary(name, code, basic): anEmployeereference holding aSalaryobject. - The
Salaryconstructor callssuper(n, c, b), which setsname,empCodeandbasicPay. It then calculates:da = basicPay * 40 / 100hra = basicPay * 20 / 100pf = basicPay * 12 / 100gross = basicPay + da + hra
maincallsob.display(). BecauseSalaryoverrides it,Salary.display()runs: it prints the heading, callssuper.display()for the name, code and basic pay, prints DA, HRA, PF and gross pay, and printsnetPay().Salary.netPay()(which overridesEmployee.netPay(), returning only the basic pay) returnsgross - pf.
Program
Employee holds the common data, with netPay() and display(). Salary extends it, calculates the allowances in its constructor and overrides netPay() and display().
import java.util.Scanner;
/**
* Base class Employee: holds the details common to every employee.
*/
class Employee
{
String name; // name of the employee
int empCode; // employee code
double basicPay; // basic pay per month in rupees
/**
* Parameterised constructor.
* @param n name of the employee
* @param c employee code
* @param b basic pay
*/
Employee(String n, int c, double b)
{
name = n;
empCode = c;
basicPay = b;
}
/**
* Pay received by the employee; a plain Employee gets only the basic pay (overridden in Salary).
* @return the pay
*/
double netPay()
{
return basicPay;
}
/** Prints the details common to every employee (overridden in Salary). */
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
/**
* Parameterised constructor; passes the common details to the base class with super(...).
* Calculates DA (40% of basic), HRA (20% of basic), PF (12% of basic) and gross pay.
* @param n name of the employee
* @param c employee code
* @param b basic pay
*/
Salary(String n, int c, double b)
{
super(n, c, b);
da = basicPay * 40 / 100;
hra = basicPay * 20 / 100;
pf = basicPay * 12 / 100;
gross = basicPay + da + hra;
}
/**
* Overrides netPay(): gross pay less the PF deduction.
* @return the net pay
*/
double netPay()
{
return gross - pf;
}
/** Overrides display(): prints the pay slip, the common details first (super.display()). */
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: " + netPay());
}
}
/**
* Driver class: reads the details and prints the pay slip through an Employee reference.
*/
public class PaySlip
{
/** Entry point: reads the input, creates the object and calls the overridden display() through ob. */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // reads the typed input
System.out.print("Enter name: ");
String name = sc.nextLine(); // name of the employee
System.out.print("Enter employee code: ");
int code = sc.nextInt(); // employee code
System.out.print("Enter basic pay: ");
double basic = sc.nextDouble(); // basic pay
if (basic > 0)
{
Employee ob = new Salary(name, code, basic); // base class reference, derived class object
ob.display();
}
else
{
System.out.println("INVALID INPUT");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
name | String | Data member of Employee: name of the employee; also a local variable in main holding the typed name |
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 |
n | String | Constructor parameter: name of the employee |
c | int | Constructor parameter: employee code |
b | double | Constructor parameter: basic pay |
sc | Scanner | Local variable in main: reads the typed input |
code | int | Local variable in main: employee code typed by the user |
basic | double | Local variable in main: basic pay typed by the user |
ob | Employee | Base class reference in main, holding a Salary object |
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 first sample: name Anita Sharma, code 101, basic 25000.
| Step | Statement | Value |
|---|---|---|
| 1 | basic > 0 is true, so new Salary(name, code, basic) runs super(n, c, b) | basicPay = 25000 |
| 2 | da = basicPay * 40 / 100 | da = 10000 |
| 3 | hra = basicPay * 20 / 100 | hra = 5000 |
| 4 | pf = basicPay * 12 / 100 | pf = 3000 |
| 5 | gross = basicPay + da + hra = 25000 + 10000 + 5000 | gross = 40000 |
| 6 | ob.display() runs Salary.display(), which calls netPay(): gross - pf = 40000 - 3000 | Net pay: 37000.0 |
The net pay is 37000.0, as in the first sample run.
Variations
- Different employee types "Write classes for Manager and Clerk derived from Employee, each overriding
netPay()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."