Sh0gun
Sh0gun

Reputation: 931

Inheritance Returning null values

So I've been working with inheritance and I've run into a problem. I'm very new to the super and extends commands and there is porbably an obvious error with syntax that I am just missing. Basically I have 5 classes, one super, three subs and 1 tester class. Everything looked good until I tried to run the tester class, and where I hoped to find the data entered when instantiating the variables, I instead got null values back. I need to know if this is a problem with how I declared them, or if it's simply a problem with my super and sub classes. Any advice you can give for this would be greatly appreciated.

In an attempt not to fill up the screen, I've included my super and sub classes in this paste bin:

Super: http://pastebin.com/eZLXvknz

Sub1: http://pastebin.com/PFgApK3Y

Sub2: http://pastebin.com/QKDjB9g4

Here is my tester class:

public class EmployeeTest {


public static void main(String[] args){

 Employee [] employeeArray = new Employee[3] ; 
 SalariedEmployee employee1 = new SalariedEmployee("Andrea", "Doroshenko", "111-111-111", 6, 2011, 2400); 
 CommissionedEmployee employee2 = new CommissionedEmployee("Nick", "McRae", "222-222-222", 1, 1998, 50000, 0.1);
 SalPlusCommEmployee employee3 = new SalPlusCommEmployee("Dan", "Mills", "333-333-333", 3, 2011, 1000, 0.05, 500 );

    employeeArray[0] = employee1;
    employeeArray[1] = employee2;
    employeeArray[2] = employee3;

    System.out.println(employee1.getEmployeeDetails());
    System.out.println(employee2.getEmployeeDetails());
    System.out.println(employee3.getEmployeeDetails());


}

//end main
}
//end class

I figure that the porblem is either going to be with the tester or the super, but included the others just in case. I know that right now the array seems pointless, but I will need it later on, so that's why it's there. Thanks again for any help you guys can give!

Upvotes: 0

Views: 2492

Answers (3)

Guillaume Polet
Guillaume Polet

Reputation: 47608

The constructor of your super class never assigns the values it gets. There is no (for example):

this.firstName = firstName;

Upvotes: 1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

First off: get all that static crap out of your classes. They don't belong and they're messing you up royally.

e.g.,

public class CommissionedEmployee extends Employee
{
    static double commRate;
    static double salesMade;

    public CommissionedEmployee(String firstName, String lastName, String SINNumber, int startMonth, int startYear, double salesMade, double commRate)
    {
        super(firstName, lastName, SINNumber, startMonth, startYear);
    }

    public static double getcommRate()
    { 
        return commRate;
    }
    public static double setcommRate()
    { 
        return commRate = setcommRate();
    }

    public static double getSalesMade()
    { 
        return salesMade;
    }
    public static double setsalesMade()
    { 
        return salesMade = setsalesMade();
    }

    public String getEmployeeDetails()
    {
        return ("This Employee is " + firstName + lastName + " this employee has the SIN " + sinNumber + "and is " + gender + ". This is a commisioned employee");
    }

    public double getEarnings()
    {
        return commRate * salesMade;
    }

}

When you make a variable static, you are saying that this variable will hold information that represents the state of the class, not objects of the class. So each object will hold the same static value always, and this is not what you want. You want a CommissionedEmployee to have his own commRate and his own salesMade value, and declaring these values and their related methods static will ruin this for you.

Upvotes: 1

Juvanis
Juvanis

Reputation: 25950

In your Employee constructor, you do not initialize employee features. That's all. Initialize them.

Upvotes: 2

Related Questions