Hi I am having problems of how to get access from variables in a super class o a subclass.
Before somebody ask? homework yes… do I want you to do it for me? Nope, I want to learn but I am really stuck here, I understand the concept of Composition, I see an exercise on the book and I understand perfectly what is doing…
Here is what I got:
1) I have a super class that is in Jar file, I created a link in Eclipse, I know that the link is created correctly, I am going to concentrate just in one
variable, so I don’t have to put all the code here firstName;
in the super class(the one that is define in the path)
public class CommissionEmployee {
// Field descriptor #6 Ljava/lang/String;
private java.lang.String firstName;
in my class i have 6 argument constructor
// six-argument constructor
public EmployeeComposition( CommissionEmployee first,
String last,
String ssn, double sales, double rate, double salary )
{
// super();
setBaseSalary( salary);
setGrossSales( sales);
}
Notice how I define first...CommissionEmployee first,
then I Initialize
FirstName = first;
then I have this methods, they are set up wrong, I need to be able to use the variable firstName from the super class
commission employee and Initialize first to fisrtName..
I (EmployeeComposition) have access to the method emp.getFirstName(); but not to the variable
//getting first name
public CommissionEmployee getFirstName() {
return FirstName;
}
I also try (it is also wrong)
public void set getFirstName() {
return FirstName;
}
public void setFirstName(String first) {
// String firstName = null;
first = emp.getFirstName(); /// I can’t do something like first = emp.FirstName.
}
I am able to access the objects of the CommissionEmployee but not the variables
This is what I have in the header of the class
public class EmployeeComposition
{
//this.CommissionEmployee=new CommissionEmployee();
private double baseSalary; // base salary per week
double commissionRate;
double grossSales;
private CommissionEmployee FirstName;
public CommissionEmployee emp;
when I run the program
I got this error, I have a
unit test... a
public void testSetFirstName() {
// Only checking the nominal case (this method should be delegated to an already-tested CommissionEmployee anyway...)
final String altFirstName = "Bruce";
emp.setFirstName(altFirstName);
try {
assertEquals("setFirstName did not set the value as expected", altFirstName, commissionEmployeeReference.getFirstName());
} catch (Exception ex) {
assertThrowableTestFailure(ex);
}
}
Any ideas??
I guess I need to know how I need to set up my subclass to access the variables from the superclass
My understanding is that in composition you can’t do extension ‘
smoothening like this
public class BasePlusCommissionEmployeeComposition extension CommissionEmployee
when I do extension I can define the constructor of the superclass like this
Y
super(ssn, sales, rate, salary );
but when I take the extension out
only like this super()