Home > Software design >  Inherited objects outputs as null
Inherited objects outputs as null

Time:03-16

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

import java.util.Scanner;

public class Employee {
    String name;
    
    public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
       Employee emp = new Employee();
       FullTimeEmployee fte = new FullTimeEmployee();
       PartTimeEmployee pte = new PartTimeEmployee();
       
       
        System.out.println("Please input your name: ");
        String Empname = input.nextLine();
        
        emp.setName(Empname);
        
        System.out.println(emp.getName());
        
        
        
        System.out.println("Press F for Full Time or P for Part Time: ");
        char pftype = input.next().charAt(0);
        
        switch (pftype) {
            case 'P','p':
                pte.PartTimeEmployee();
                break;
                
            case 'F', 'f':
                fte.FullTimeEmployee();
                break;
             
            default : 
                System.out.println("Invalid Output");
                break;
        
        }
    }
        
    public void setName(String name){
        this.name = name;
} 
    
    public String getName(){
        return name;
    }
    
    
}

Sub classes

import java.util.Scanner;

    public class FullTimeEmployee extends Employee{
    double monthlySalary;
    String name;
    
    public void FullTimeEmployee() {
        
        FullTimeEmployee fte = new FullTimeEmployee();
        
    
        
        fte.writeOutput();
        
        
         System.out.println("______________________________________");
         System.out.println("Employee name:  "  fte.getName());
         System.out.println("Monthly Salary: "  fte.getMonthlysalary());
        
    }
    
    
    
    public void writeOutput() {
    Scanner input = new Scanner(System.in);
        
    System.out.println("Please enter your monthly salary: ");
        double FEsalary = input.nextDouble();
        setMonthlySalary(FEsalary);
}
    
    public void setMonthlySalary(double monthlySalary){
        this.monthlySalary = monthlySalary;
    }
    
    public double getMonthlysalary(){
        return monthlySalary;
    }
    
   
}
import java.util.Scanner;

public class PartTimeEmployee extends Employee {
    double ratePerHour, wage;
    int hoursWorked;
    String name;
    
    public void PartTimeEmployee() {
       PartTimeEmployee pte = new PartTimeEmployee();
       String name = pte.getName();
       pte.readInput();
       System.out.println("______________________________________");
         System.out.println("Employee name:  "  pte.getName());
         System.out.println("Monthly Salary: "  pte.getWage());
       
        
    }
    
    
    public void readInput(){
        Scanner input = new Scanner(System.in);
        
        System.out.println("Please enter your rate per hour and number of hours worked seperated by a space: ");
        String[] salary = input.nextLine().split(" "); //SCAN THE INPUT OF THE USER AND STORE THEM IN AN ARRAY. SPLITS THE WHITE SPACE.
       
       try {  //TRY THE CODE AND BE TESTED FOR ERRORS BEFORE BEING EXECUTED
       ratePerHour = Double.parseDouble(salary[0]);
       hoursWorked = Integer.parseInt(salary[1]);
       setWage(ratePerHour, hoursWorked);
       } catch (Exception e) { // CATCH THE STATEMENT IF AN ERROR OCCURED ON THE CODE
           System.out.println("Invalid input");
           System.exit(0);
       }
}
    
    public void setWage(double ratePerHour, int hoursWorked){
    wage = ratePerHour * hoursWorked;
}
    public double getWage(){
        return wage;
    }
    
  
    
    
}

OUTPUT OF EITHER

Employee name:  null
Monthly Salary: 1110.0

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!

CodePudding user response:

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.

 emp.setName(Empname);

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.

 String name; //delete this from your derived classes

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

System.out.println("Employee name:  "  getName()); // just getName() from base class
System.out.println("Monthly Salary: "  pte.getWage());

CodePudding user response:

Your derived classes have their own names, separate from the base class' name. When you assign this.name you initialize the 'local' class member and the parent class' name remains null.

  •  Tags:  
  • java
  • Related