• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Composition: has a relationship

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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()
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Let's see, I see a number of issues.

Rogelio Echeverri wrote:
I also try (it is also wrong)
public void set getFirstName() {
return FirstName;

}


Do you understand why this is wrong? First of all, the first line contains an extra word. A method declaration has these parts:

<access specifier> <return type> <methodname>(arguments)

In your case:

  • access specifier = public
  • return type = void (which means this method does not return anything)
  • an extra word "set" which does not belong in there
  • method name = getFirstName
  • arguments = () (which means no arguments)

  • Then the method has a "return" statement, while you just specified the return type "void" - which means the method does not return anything. You cannot return a value from a void method.

    Rogelio Echeverri wrote:
    I am able to access the objects of the CommissionEmployee but not the variables


    Because the member variables of CommissionEmployee are private, which means you cannot access them directly outside the class CommissionEmployee. Probably class CommissionEmployee has getter and setter methods to get and set the firstName of a CommissionEmployee. You should call those instead of trying to access the member variable directly.

    Rogelio Echeverri wrote:
    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;


    There are some strange things in here. Why is there a variable FirstName of the type CommissionEmployee? A name is not a CommissionEmployee, is it? Why do you have a second variable emp, which is also a CommissionEmployee? And why is emp public?
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Rogelio Echeverri wrote:Hi I am having problems of how to get access from variables in a super class [to] a subclass...


    It sounds like you're overthinking this. You also seem to have written an incredible amount of code without any real plan, and you're now consequently tied in knots.

    My advice: Forget about all those other fields for the moment and concentrate on that "has-a" relationship (and I'm assuming here):
    EmployeeComposition HAS-A CommissionEmployee

    That means, as you appear to understand, that each EmployeeComposition object will contain a CommissionEmployee, viz:but, since CommissionEmployee already HAS a first name, adding a field in this class called 'firstName' is just duplication.

    What you need is a way to get/set the first name for your EmployeeComposition object by using the CommissionEmployee it contains, and that's usually done by a method called a "forwarder".

    Presumably, even though your CommissionEmployee class defines 'firstName' as private - which is absolutely correct - I'll bet that it contains a couple of public methods, viz:
      public String getFirstName() { ... (Note that it returns a String, not a CommissionEmployee)
    and
      public void setFirstName(String newFirstName) { ...
    which allow you to get and set the field, even though it's defined as 'private'.

    So, simply use them in your "composite" class:Do you follow? Methods like this are called "forwarders" because they "forward" the request to the 'contained' object; and they almost always look identical to the method they call.

    A few tips for the future:

    1. Try as hard as you possibly can to do ONE THING AT A TIME; and don't start adding anything new until you know that it works...EVERY TIME.

    2. Compile OFTEN:
  • Every 10 lines you write.
  • Every field you add.
  • Every method you complete.
  • And don't stop compiling until you have cleared every error before you continue.

    3. Always, always, always make your fields private - unless you've been specifically told not to (and if you have, you might want to get a new book ... or teacher).

    In the example above, you only need ONE field to start off with (emp). You can worry about adding the others once you know that 'emp' (ie, your "has-a" relationship) works ... and I'll leave that up to you.

    HIH

    Winston
     
    Rogelio Echeverri
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you SO much this is helpful, I will try it later
     
    reply
      Bookmark Topic Watch Topic
    • New Topic