Skip to content
Advertisement

Inherited objects outputs as null

I have Employee as my main class (used to get the name and call other methods). I created two inheritance class called FullTimeEmployee and PartTimeEmployee. The program is working except the getName() in my subclasses. The name the I input works in the main class but shows up as null in the subclasses. It always outputs null and I don’t know what is wrong. Can you tell me what’s wrong and appreciate the help to fix it?

Main Class

JavaScript

Sub classes

JavaScript
JavaScript

OUTPUT OF EITHER

JavaScript

I’m pretty new at coding and just started learning so please excuse if my code is very redundant or has errors. Appreciate the help!

Advertisement

Answer

You are not setting the empName anywhere to your inherited class objects i.e. FullTimeEmployee and PartTimeEmployee.

You are setting empName to only employee object.

JavaScript

There are 3 different kind of objects here Employee(), PartTimeEmployee(), and FullTimeEmployee(). You are setting name only on Employee object. One way to do it is omit the re-declaration of name in your derived classes.

JavaScript

And just use the name of your parent class. i.e.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement