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

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

  1. main (in the driver class PaySlip) reads name, code and basic.
  2. If basic is not greater than 0, it prints INVALID INPUT and stops.
  3. Otherwise it executes Employee ob = new Salary(name, code, basic): an Employee reference holding a Salary object.
  4. The Salary constructor calls super(n, c, b), which sets name, empCode and basicPay. It then calculates:
    • da = basicPay * 40 / 100
    • hra = basicPay * 20 / 100
    • pf = basicPay * 12 / 100
    • gross = basicPay + da + hra
  5. main calls ob.display(). Because Salary overrides it, Salary.display() runs: it prints the heading, calls super.display() for the name, code and basic pay, prints DA, HRA, PF and gross pay, and prints netPay().
  6. Salary.netPay() (which overrides Employee.netPay(), returning only the basic pay) returns gross - 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

VariableData TypePurpose
nameStringData member of Employee: name of the employee; also a local variable in main holding the typed name
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
nStringConstructor parameter: name of the employee
cintConstructor parameter: employee code
bdoubleConstructor parameter: basic pay
scScannerLocal variable in main: reads the typed input
codeintLocal variable in main: employee code typed by the user
basicdoubleLocal variable in main: basic pay typed by the user
obEmployeeBase class reference in main, holding a Salary object
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 first sample: name Anita Sharma, code 101, basic 25000.

StepStatementValue
1basic > 0 is true, so new Salary(name, code, basic) runs super(n, c, b)basicPay = 25000
2da = basicPay * 40 / 100da = 10000
3hra = basicPay * 20 / 100hra = 5000
4pf = basicPay * 12 / 100pf = 3000
5gross = basicPay + da + hra = 25000 + 10000 + 5000gross = 40000
6ob.display() runs Salary.display(), which calls netPay(): gross - pf = 40000 - 3000Net pay: 37000.0

The net pay is 37000.0, as in the first sample run.

Variations

  1. Different employee types "Write classes for Manager and Clerk derived from Employee, each overriding netPay() 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."