Constructors

I have a problem with the output of this program

this program consist of three classes

the fist one is

package c;

publicclass CommissionEmployee4

{

private String firstName;

private String lastName;

private String socialSecurityNumber;

privatedouble grossSales;// gross weekly sales

privatedouble commissionRate;// commission percentage

// five-argument constructor

public CommissionEmployee4( String first, String last, String ssn,

double sales,double rate )

{

// implicit call to Object constructor occurs here

firstName = first;

lastName = last;

socialSecurityNumber = ssn;

setGrossSales( sales );// validate and store gross sales

setCommissionRate( rate );// validate and store commission rate

System.out.printf(

"\nCommissionEmployee4 constructor:\n%s\n",this );

}// end five-argument CommissionEmployee4 constructor

// return first name

public String getFirstName()

{

return firstName;

}// end method getFirstName

// return last name

public String getLastName()

{

return lastName;

}// end method getLastName

// return social security number

public String getSocialSecurityNumber()

{

return socialSecurityNumber;

}// end method getSocialSecurityNumber

// set gross sales amount

publicvoid setGrossSales(double sales )

{

grossSales = ( sales < 0.0 ) ? 0.0 : sales;

}// end method setGrossSales

// return gross sales amount

publicdouble getGrossSales()

{

return grossSales;

}// end method getGrossSales

// set commission rate

publicvoid setCommissionRate(double rate )

{

commissionRate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0;

}// end method setCommissionRate

// return commission rate

publicdouble getCommissionRate()

{

return commissionRate;

}// end method getCommissionRate

// return String representation of CommissionEmployee4 object

public String toString()

{

return String.format("%s: %s %s\n%s: %s\n%s: %.2f\n%s: %.2f",

"commission employee", getFirstName(), getLastName(),

"social security number", getSocialSecurityNumber(),

"gross sales", getGrossSales(),

"commission rate", getCommissionRate() );

}// end method toString

}// end class CommissionEmployee4

the second class is inherited from the fist and inside its constructor ,explicitly call the construcror of the super class

package c;

publicclass BasePlusCommissionEmployee5extends CommissionEmployee4

{

privatedouble baseSalary;// base salary per week

// six-argument constructor

public BasePlusCommissionEmployee5( String first, String last,

String ssn,double sales,double rate,double salary )

{

super( first, last, ssn, sales, rate );

setBaseSalary( salary );// validate and store base salary

System.out.printf(

"\nBasePlusCommissionEmployee5 constructor:\n%s\n",this );

}// end six-argument BasePlusCommissionEmployee5 constructor

// set base salary

publicvoid setBaseSalary(double salary )

{

baseSalary = ( salary < 0.0 ) ? 0.0 : salary;

}// end method setBaseSalary

// return base salary

publicdouble getBaseSalary()

{

return baseSalary;

}// end method getBaseSalary

// return String representation of BasePlusCommissionEmployee5

public String toString()

{

return String.format("%s %s\n%s: %.2f","base-salaried",

super.toString(),"base salary", getBaseSalary() );

}// end method toString

}// end class BasePlusCommissionEmployee5

the third class is that contain the main method and print and show how the contructors of the subclass call the constructor of the superclass

package c;

publicclass ConstructorTest{

publicstaticvoid main(String[] args){

CommissionEmployee4 employee1 =new CommissionEmployee4(

"Bob","Lewis","333-33-3333", 5000, .04 );

System.out.println();

BasePlusCommissionEmployee5 employee2 =

new BasePlusCommissionEmployee5(

"Lisa","Jones","555-55-5555", 2000, .06, 800 );

System.out.println();

BasePlusCommissionEmployee5 employee3 =

new BasePlusCommissionEmployee5(

"Mark","Sands","888-88-8888", 8000, .15, 2000 );

}

}

But the problem with me is abou this output

**************************the output*****************************

CommissionEmployee4 constructor:

commission employee: Bob Lewis

social security number: 333-33-3333

gross sales: 5000.00

commission rate: 0.04

CommissionEmployee4 constructor:

base-salaried commission employee: Lisa Jones

social security number: 555-55-5555

gross sales: 2000.00

commission rate: 0.06

base salary: 0.00

BasePlusCommissionEmployee5 constructor:

base-salaried commission employee: Lisa Jones

social security number: 555-55-5555

gross sales: 2000.00

commission rate: 0.06

base salary: 800.00

CommissionEmployee4 constructor:

base-salaried commission employee: Mark Sands

social security number: 888-88-8888

gross sales: 8000.00

commission rate: 0.15

base salary: 0.00

BasePlusCommissionEmployee5 constructor:

base-salaried commission employee: Mark Sands

social security number: 888-88-8888

gross sales: 8000.00

commission rate: 0.15

base salary: 2000.00

************************************************** **************

why the object employee2 print this statment> (base salary: 0.00)

i know that when we create the employee2 object:first it call the CommissionEmployee4 constructor so it prints its values (i.e. "Lisa", "Jones", "555-55-5555", 2000, .06, 800 ) so why it print the base salary although there isn't in the CommissionEmployee4 constructor base salary

[10757 byte] By [eMerooa] at [2007-11-26 20:46:54]
# 1

> so why it print the

> base salary although there isn't in the

> CommissionEmployee4 constructor base salary

Because the constructor ends up also invoking the toString() method, which is overridden in your derived class.

You really should remove the println junk out of the constructors anyway. Constructors shouldn't invoke polymorphic methods which depend on object state, because the object is still under construction until after the constructor ends.

warnerjaa at 2007-7-10 2:09:21 > top of Java-index,Java Essentials,New To Java...
# 2

sorry i can't get it completely

When I write this statement

BasePlusCommissionEmployee5 employee2 =

new BasePlusCommissionEmployee5(

"Lisa", "Jones", "555-55-5555", 2000, .06, 800 );

first it call the BasePlusCommissionEmployee5's constructor

that it by turn call the CommissionEmployee4's constructor in reponse to this statment>

super( first, last, ssn, sales, rate );

super reference print the statment in its contructor to print this output

CommissionEmployee4 constructor:

base-salaried commission employee: Lisa Jones

social security number: 555-55-5555

gross sales: 2000.00

commission rate: 0.06

why it dosen't like that but print also base salary: 0.00

after that all it execute the statment of

System.out.printf(

"\nBasePlusCommissionEmployee5 constructor:\n%s\n", this );

to print this

BasePlusCommissionEmployee5 constructor:

base-salaried commission employee: Mark Sands

social security number: 888-88-8888

gross sales: 8000.00

commission rate: 0.15

base salary: 2000.00

that's the order i know it to execute the program.Is it right?

eMerooa at 2007-7-10 2:09:21 > top of Java-index,Java Essentials,New To Java...
# 3

CommissionEmployee4's constructor does this part:

> System.out.printf(

> "\nCommissionEmployee4 constructor:\n%s\n", this );

See the bolded part?

"this" is actually a BasePlusCommissionEmployee5 object reference, remember.

System.out.printf(String s, Object o) ends up invoking o.toString(). Since o is a BasePlusCommissionEmployee5 reference, it is invoking BasePlusCommissionEmployee5's toString() method, not the method you think it is.

warnerjaa at 2007-7-10 2:09:21 > top of Java-index,Java Essentials,New To Java...
# 4

Thank you for this notation now I got it.

But this mean that the statment

super( first, last, ssn, sales, rate );

don't take a copy from these argument and execute the constructor to print its output there

BUT it inherits the members of CommissionEmployee4

and copy the constructor to work in BasePlusCommissionEmployee5

so the this

reference points to BasePlusCommissionEmployee5's toString().

Right?

eMerooa at 2007-7-10 2:09:21 > top of Java-index,Java Essentials,New To Java...
# 5

> Thank you for this notation now I got it.

> But this mean that the statment

> super( first, last, ssn, sales, rate );

> don't take a copy from these argument and execute the

> constructor to print its output there

> BUT it inherits the members of CommissionEmployee4

> and copy the constructor to work in

> BasePlusCommissionEmployee5

> so the this

reference points to

> BasePlusCommissionEmployee5's toString().

> Right?

This post confuses me. I believe I have explained the issue the best I can. Good luck.

warnerjaa at 2007-7-10 2:09:21 > top of Java-index,Java Essentials,New To Java...