Results 1 to 4 of 4
  1. #1
    SVTermiCobra is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default how to display from toString through subclass

    hello, I am new to Java and everything has been going pretty smoothly until now. I am having trouble trying to access a toString() method from another class. I have 3 classes Employee, Benefit, and Main. To post the entire source code would be lengthy so I will just post the code directly related to the issue. This is the toString() inside my Benefits class:

    I've also created objects as follows to pass through these:
    Benefit benefit1 = new Benefit(a, b, c);
    Employee employee1 = new Employee(1, 2, 3);

    METHOD

    public String toString() {
    return String.format("Health Insurance: %s\nLife Insurance: %s\nVacation: %s\n", getHealthInsurance(), getLifeInsurance(), getVacation());
    }

    If I insert System.out.printf("%s", this); within the Benefit class, then it will display. But I want it to display this along with this in the Employee class...

    CONSTRUCTOR

    public Employee(String first, String last, char gen, int dep, double salary, Benefit Benefit) {

    // the Employee constructor with input

    firstName = first;
    lastName = last;
    gender = gen;
    dependents = dep;
    annualSalary = salary;
    benefit = Benefit;
    numEmployees++;

    System.out.printf("%s", this); //this displays a toString method in the Employee class

    }

    So in the Employee Class, how do I call the toString method from the benefits class?

    SOLVED

    Java Code:
    Benefit benefit1 = new Benefit("Full", 2000, 10);
    		Employee employee1 = new Employee("George", "Anderson", 'M', 5, 42000, benefit1);
    		System.out.printf("%s", employee1.toString());
    		System.out.printf("%s", benefit1.toString());
    Last edited by SVTermiCobra; 08-07-2012 at 10:42 PM.

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: how to display from toString through subclass

    I'm not sure I understood your question correctly. If not, ignore my reply.

    Look into inheritance http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html.

    Your code would look something like for the Benefit class:

    Java Code:
    public class Benefit extends Employee {
    which will result in that the methods from Employee class are inherited to the Benefit class.

    ---

    Off topic tip, use the [CODE]-brackets to display code easier. This button how to display from toString through subclass-k6gsn.png .
    Last edited by Zyril; 08-07-2012 at 10:31 PM.

  3. #3
    SVTermiCobra is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Re: how to display from toString through subclass

    Thanks for the tip Zyril! I'll keep that code bracket in mind. I was trying to figure out how to call the toString method from Benefit to the Main class. I figured it out though!

    I created the following objects:

    Java Code:
    Benefit benefit1 = new Benefit("Full", 2000, 10);
    Employee employee1 = new Employee("George", "Anderson", 'M', 5, 42000, benefit1);
    System.out.printf("%s", employee1.toString()); //this was what I was trying to achieve, to display the object's information from the toString method in their class.
    System.out.printf("%s", benefit1.toString());
    problem solved by myself so looks like this is a good start. but I'm sure I will have more difficult issues to address here in the next few hours!

  4. #4
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: how to display from toString through subclass

    Ah, some times it's easy to look over simple solutions! Glad it worked out! =)

Similar Threads

  1. Replies: 1
    Last Post: 12-01-2010, 06:35 PM
  2. Replies: 1
    Last Post: 05-31-2010, 08:46 AM
  3. Subclass help
    By amystauff in forum New To Java
    Replies: 1
    Last Post: 05-30-2010, 04:36 AM
  4. Replies: 8
    Last Post: 12-29-2008, 07:12 AM
  5. Subclass definition
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •