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

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

  1. main (in the driver class PaySlip) creates an object ob of the derived class Salary. The default constructors set the data members to 0 or empty.
  2. main calls ob.accept(). Salary.accept() calls super.accept(), which reads name, empCode and basicPay and returns true only if basicPay > 0.
  3. If it returned false, main prints INVALID INPUT and stops.
  4. Otherwise main calls ob.calculate(), which finds:
    • da = basicPay * 40 / 100
    • hra = basicPay * 20 / 100
    • pf = basicPay * 12 / 100
    • gross = basicPay + da + hra
    • net = gross - pf
  5. main calls ob.display(). Salary.display() prints the heading, calls super.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

VariableData TypePurpose
scScannerData member of Employee: reads the typed input; inherited by Salary
nameStringData member of Employee: name of the employee
empCodeintData member of Employee: employee code
basicPaydoubleData member of Employee: basic pay per month
dadoubleData member of Salary: dearness allowance, 40% of basic pay
hradoubleData member of Salary: house rent allowance, 20% of basic pay
pfdoubleData member of Salary: provident fund deduction, 12% of basic pay
grossdoubleData member of Salary: gross pay, basic pay plus DA plus HRA
netdoubleData member of Salary: net pay, gross pay minus PF
obSalaryObject of the derived class created in main
argsString[]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.0

Sample 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.0

Sample 3

Enter name: Kavya Nair
Enter employee code: 103
Enter basic pay: 0
INVALID INPUT

Sample 4

Enter name: Dev Patel
Enter employee code: 104
Enter basic pay: -5000
INVALID INPUT

One value per line

Dry run

Take the second sample: name Rohan Mehta, empCode 102, basicPay 1000.

StepStatementValue
1ob.accept() reads the details and basicPay > 0basicPay = 1000, returns true
2da = basicPay * 40 / 100da = 400
3hra = basicPay * 20 / 100hra = 200
4pf = basicPay * 12 / 100pf = 120
5gross = basicPay + da + hra = 1000 + 400 + 200gross = 1600
6net = gross - pf = 1600 - 120net = 1480
7ob.display() prints the pay slipNet pay: 1480.0

The net pay is 1480.0, as in the second sample run.

Variations

  1. Different employee types "Write classes for Manager and Clerk derived from Employee, each with its own allowance percentages."
  2. Income tax "Deduct income tax from the gross pay at a rate that depends on the gross pay."
  3. Many employees "Read the details of n employees and print the pay slip of each."