Hi all,

I have created an abstract class Named Employee and one Sub-Class named FullTimeEmployee. In my abstract class "Employee", i have defined two constructors one without parameter and one with two parameter, both used to intializee first name and last name. My aim is to print payroll for employee in a file.

My problem is, my program works fine that is i get the desired output in file when i have two constructors in the sub-class FullTimeEmployee, but when i add another that is third constructor in the subclass, i get the firstname and second name in the file from the first constructor (without parameter) of abstract eventhough i call the second one.First constructor i just intialised the names to sample value.

Output i get when having two constructors in subclass is

daniel
creig
10000
2350

output i get when having three constructors in subclass is

Null
Null
10000
2350

Please explain this behaviour, i have posted the subclass with three constructors 1. no para 2. first,lastname 3.basesalry, hours worked. if i have two constructors in sub class like 1. no para 2. first,last,basesalary,hoursworked and call the constructor accordingly it works perfect. why ?

public abstract class Employee {

    /**
     * @param args
     */
    private String firstName;
    private String lastName;


    public  Employee (){

         firstName = "test";
         lastName = "testone";
    }

    public Employee(String inFirstName, String inLastName){
        firstName = inFirstName;
        lastName = inLastName;

    }

    public String getFirstName(){

        return firstName;
    }

    public void setFirstName(String inFirstName){

          firstName =  inFirstName; 
    }


   public String getLastName(){

        return lastName;
    }

    public void setLastName(String inLastName){

          lastName =  inLastName; 
    }

    public abstract double compensation();

    public abstract String paystub();

    public String toString(){

        return paystub();
    }

}

Sub Class

import java.text.DecimalFormat;

public class FullTimeEmp extends Employee {


    private double baseSalary;
    private int hoursWorked;


    public  FullTimeEmp(){

        super();
        baseSalary = 0.0;
        hoursWorked = 0;

    }

    public FullTimeEmp(String inFirstName, String inLastName,double)
    {

        super(inFirstName,inLastName);
        baseSalary = inBaseSalary;
        hoursWorked = inHoursWorked;



    }

    public FullTimeEmp(double inBaseSalary, int inHoursWorked)
    {
        baseSalary = inBaseSalary;
        hoursWorked = inHoursWorked;        
    }


    @Override
    public double compensation() {
        // TODO Auto-generated method stub
        //return 0;

        return (baseSalary + (1.5 * (80 - hoursWorked)));

    }

    @Override
    public String paystub() {
        // TODO Auto-generated method stub
        //return null;

        double payroll;
        String salary;
        DecimalFormat currencyFormat = new DecimalFormat("0.00");

        payroll = compensation();

        salary = "                  Heartland Cars of America" +"\n\n                          FulltimeEmployee" + "\n\n" + "\t\t\t\tfirst name    : " + super.getFirstName() + "\n" + "\t\t\t\tLast name     : " + super.getLastName() + "\n" + "\t\t\t\tBase Salary   : " + baseSalary + "\n" + "\t\t\t\tHours worked  : " + hoursWorked + "\n" + "\t\t\t\tSalary        : " + currencyFormat.format(payroll)+"\n\n";
        return salary;
    }

}

Application Program

import java.io.*;
import java.util.*;

public class Payroll {

    /**
     * @param args
     */
    public static void main(String[] args)throws FileNotFoundException, IOException {
        // TODO Auto-generated method stub

        Scanner scannedInfo = new Scanner(new File("D:\\apple.dat"));
        PrintWriter output = new PrintWriter(new FileWriter("D:\\orange.dat"));
        Employee reference ;
        String firstName;
        String lastName;
        Double baseSalary;
        int hoursWorked;

        String choice;



        while (scannedInfo.hasNext())
        {
            choice = scannedInfo.next();

            switch(choice){
            case "F":
            case "f":

            firstName = scannedInfo.next();
            lastName = scannedInfo.next();
            baseSalary = scannedInfo.nextDouble();
            hoursWorked = scannedInfo.nextInt();
            reference = new FullTimeEmp(firstName,lastName);
            reference = new FullTimeEmp(baseSalary,hoursWorked);
            //reference = new FullTimeEmp(baseSalary,hoursWorked);
            output.print(reference.paystub());


            break;  

            default :
            break;  
        }   
        }

        scannedInfo.close();
        output.close();


    }

}

do you understand why this:

reference = new FullTimeEmp(firstName,lastName);
reference = new FullTimeEmp(baseSalary,hoursWorked);

is pointless coding?

The constructor you posted won't compile, so maybe post the right version?

 public FullTimeEmp(String inFirstName, String inLastName,double)
 {

@OP could you please include the .dat files, I wanna try a few things. ALso you could use the same constructors. For example, say you had a constructor that took an employee's name and an employee's payrate. No need to make another constructor with those parameters, you could use super to refer to those and include a new parameter, such as employee's id. Just tossing some ideas out there.

@james Cherill,
Sorry that was a typo error. In the actual program, there is no double in the second constructor of the subclass.

@Patrick,stultuske ,
I understand that i can use one constructor to intialise First,Lastname, BaseSalary, HoursWorked like

public FullTimeEmp(String firstName,String lastName, double baseSalary, int hourWorked)

What i dont understand is why cant i use two seperate constructors eventhough it is not necessary. Is it wrong to do like this ?

public FullTimeEmp(String firstName,String lastName)
public FullTimeEmp(double baseSalary, int hourWorked)

This is the content of my input File

f
Daniel
Creig
10000
2350

Murali: you obviously missed my point.
what you have in your code =

reference = new FullTimeEmp(firstName,lastName);
reference = new FullTimeEmp(baseSalary,hoursWorked);

to break this down:

STEP 1:

reference = new FullTimeEmp(firstName,lastName);

you create a new Instance of FullTimeEmp, and pass the values of firstName and lastName to it. next to that, you have the variable reference to which you link said object. (reference actually 'references' the object you just made)

STEP 2

reference = new FullTimeEmp(baseSalary,hoursWorked);

you create an entirely new object of the type FullTimeEmp, which only has the values baseSalary and hoursWorked. this new instance is not related to the instance you created with the constructor to which you passed the first- and lastname, meaning those names are not included in this instance's variables.

next, you have your variable reference becoming a reference to this new object. you no longer have a reference to your initial instance of FullTimeEmp (with the names) so you can no longer use the data in there.

commented: good +0

@ stultuske (Brinkhof Hans) ,

I got it .... im new to programming.. your explaination is very Clear...Thanks verymuch...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.