Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects (Retired) Delivering the MVP Wrapping up

Ben Morse
Ben Morse
6,068 Points

Challenge task 3 of 4 error in my code?

Task 3: Alright so now let's focus on ForumPost. It's going to need a constructor that takes a User author a String title and a String description. Don't forget to initialize the member fields in the constructor. Also please add the getter for mDescription.

Here is my code:

public class ForumPost {
  private User mAuthor;
  private String mTitle;
  private String mDescription;

  public ForumPost (User author) { 
    mAuthor = author;    
  }
  public ForumPost (String title) { 
    mTitle = title;
  }

   public ForumPost (String description) { 
    mDescription = description;
  }

  public User getAuthor() {
    return mAuthor;
  }

  public String getTitle() {
    return mTitle;
  }

  public String getDescription() {
    return mDescription;
  }
  // TODO: We need to expose the description
}

here is my ERROR:

./ForumPost.java:13: error: constructor ForumPost(String) is already defined in class ForumPost
   public ForumPost (String description) { 
          ^
1 error

I'm confused....that shouldn't be a problem because i'm passing a different variable. Right?

Hi Ben,

I added markdown to help make the code more readable. If you're curious about how to add markdown like this on your own, checkout this thread on posting code to the forum . Also, there is a link at the bottom called Markdown Cheatsheet that gives a brief overview of how to add markdown to your posts.

Cheers!

Ben Morse
Ben Morse
6,068 Points

thanks for the tip :)

2 Answers

The challenge is expecting you to create one constructor that has three parameters.

public constructorName(param1, param2, param3) {
     m1 = param1;
     m2 = param2;
     m3 = param3;
}

Currently you have three constructors, each taking one parameter.

Ben Morse
Ben Morse
6,068 Points

ah, i was doing to much then. Thanks!

author is an object, so it should be type of User. The correct answer is:

public class ForumPost {
  private User author;
  private String title;
  private String description;

  // TODO: add a constructor that accepts the author, title and description

  public ForumPost(User author, String title, String description) {
      this.author = author;
      this.title = title;
      this.description = description;
  }

  public User getAuthor() {
    return author;
  }

  public String getTitle() {
    return title;
  }

  public String getDescription() {
    return description;
  }
}