简体   繁体   中英

Inheritance & Polymorphism in Java

I have programmed for exercising the Inheritance & Polymorphism use in Java, but coming across some problems, please see the codes below:

Superclass:

public class Worker {

private String name;
private double salaryRate;

public Worker(String nm, double rate){
nm=name;
rate=salaryRate;
}

public void computePay(int hours){

double pay=hours*salaryRate;
System.out.println("The Salary for "+getName()+" is "+pay);

}

public String toString(){
return name+" "+salaryRate;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getSalaryRate() {
return salaryRate;
}

public void setSalaryRate(double salaryRate) {
this.salaryRate = salaryRate;
}
}

one subclass:

   public class HourlyWorker extends Worker {

   public HourlyWorker(String nm, double rate) {
    super(nm, rate);

    }

   public void computePay(int hours){

if (hours>40){
    double extraPay=(hours-40)*1.5*getSalaryRate();
    double pay=40*getSalaryRate();
    double total=extraPay+pay;
    System.out.println("The salary for "+getName()+" is "+total);
}
else{
    super.computePay(hours);

}
    }
    }

Another subclass:

 public class SalariedWorker extends Worker {

 public SalariedWorker(String nm, double rate){
super(nm,rate);
   }

 public void computePay(int hours){
double pay=40*getSalaryRate();
System.out.println("The salary for "+getName()+" is "+pay);

   }
   }

Main() method:

 public class Test {

public static void main(String[] args) {
    Worker a=new HourlyWorker("Tom",2.0);
    Worker b=new HourlyWorker("Lee",2.0);
    Worker c=new SalariedWorker("Pei",2.0);
    Worker d=new SalariedWorker("Joe",2.0);

    System.out.println(a+" "+b+" "+c+" "+" "+d);

    a.computePay(50);
    b.computePay(30);
    c.computePay(20);
    d.computePay(60);


}

    }

It is a bit long, thank you for your patient to read:) However, when they compile, it shows:

 null 0.0 null 0.0 null 0.0  null 0.0
 The salary for null is 0.0
 The Salary for null is 0.0
 The salary for null is 0.0
 The salary for null is 0.0

Please advise where goes wrong, thank you guys!

You assignments are reversed in the constructor. You are not setting the instance attributes values using the input params and hence those attributes always have the default values.Change this

public Worker(String nm, double rate){
nm=name;
rate=salaryRate;
}

to

public Worker(String nm, double rate){
   this.name=nm;
   this.salaryRate=rate;
}

Note: Usage of this helps you to avoid shadowing problems as well when the name of the input params and class attributes are same.

public Worker(String nm, double rate){
nm=name;
rate=salaryRate;
}

you are assigning values to local variables not to the instance variables. Therefore those variables are having their default values. for string default value is null for double it is 0.0

you should do

public Worker(String nm, double rate){
name = nm;
salaryRate = rate;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM