title: Why the inheritance class methods print 0 and null in Java?
body: <p>I have an Employee class (including fields: empId, name, methods: getEmpId, getName, setEmpId, setName). And I create an inheritance class called HourlyEmployee from Employee. HourlyEmployee class has additional fields: rate, hours, and additional methods: setRate, setHours, monthlyPayment. I create an HourlyEmployee object called he1, when setEmpId and setName are invoked, it prints 0 and null. Can you tell me what's wrong with my code? Thanks.</p>
  <pre><code>public class Employee {
      // instance variables
      private int empId;
      private String name;
  
      // constructor
      public Employee(){
  
  }
      public Employee(int empId, String name) {
          this.empId = empId;
          this.name = name;
      }
  
      // get methods
      public int getEmpId() {
          return empId;
      }
  
      public String getName() {
          return name;
      }
  
      // set methods
      public void setEmpId(int empId) {
          this.empId = empId;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  public static class HourlyEmployee extends Employee {
      private int empId;
      private String name;
      private double rate;
      private double hours;
  
      public HourlyEmployee() {
      }
  
      public HourlyEmployee (int empId, String name, double rate, double hours) {
          this.empId = empId;
          this.name = name;
          this.rate = rate;
          this.hours = hours;
      }
  
      public void setRate(double rate) {
          this.rate = rate;
      }
  
      public void setHours(double hours) {
          this.hours = hours;
      }
  
      public double monthlyPayment(){
          double monthlyPayment = rate * hours;
          return monthlyPayment;
      }
  
      public void employeeInfo(){
          System.out.println(&quot;The employee's id is: &quot; + empId);
          System.out.println(&quot;The employee's name is: &quot; + name);
          System.out.println(&quot;The hourly rate is: &quot; + rate);
          System.out.println(&quot;The employee worked &quot; + hours + &quot; for a month&quot;);
      }
  }
  
  
  public class Test {
  
      public static void main(String[] args) {
          int id;
          String name;
          double rate;
          double hours;
          double salary;
      
          // create an hourly employee object with no-arg constructor
          HourlyEmployee he1 = new HourlyEmployee ();
          // set instance variables
          he1.setEmpId(1);
          he1.setName(&quot;Jane&quot;);
          he1.setRate(15.0);
          he1.setHours(50.0);
  
          // display employee info
          he1.employeeInfo();
          // display monthly payment
          System.out.println(&quot;Monthly payment, before change, is: &quot; + he1.monthlyPayment());
      
          // change the rate
          he1.setRate(22.0);
          // display monthly payment
          System.out.println(&quot;Monthly payment, after change, is: &quot; + he1.monthlyPayment());
  </code></pre>