Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAdmire Dhodho
5,411 PointsOverriding is confusing me pliz help
Override the toString method from java.lang.Object and make it return the following information: "BlogPost: TITLE by AUTHOR"
@Override
public String toString(){
return "BlogPost: \""+mTitle + "\" -@" mAuthor;
package com.example;
import java.util.Date;
public class BlogPost {
private String mAuthor;
private String mTitle;
private String mBody;
private String mCategory;
private Date mCreationDate;
public BlogPost(String author, String title, String body, String category, Date creationDate) {
mAuthor = author;
mTitle = title;
mBody = body;
mCategory = category;
mCreationDate = creationDate;
}
public String getAuthor() {
return mAuthor;
}
public String getTitle() {
return mTitle;
}
public String getBody() {
return mBody;
}
public String getCategory() {
return mCategory;
}
public Date getCreationDate() {
return mCreationDate;
}
}
3 Answers
Jess Sanders
12,086 PointsYou are just missing a "+" before mAuthor.
Let me know if you need more help. Otherwise, select this as "Best Answer" so that everyone else knows that you have been helped. :)
Joe Goodall
3,483 PointsRight after the constructor I added:
@Override
public String toString() {
return "BlogPost: \""+mTitle + "\" -@" + mAuthor;
}
Still not working? Anybody any idea why not?
Jess Sanders
12,086 PointsThat code worked fine when I tried it just now.
Joe Goodall
3,483 PointsWorks now. Thanks.
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Pointsi had the same problem, sometimes, just need to refresh the page, seems silly, but may work...in fact i used your answer and worked perfectly..
Schwartz Prince
Courses Plus Student 2,132 PointsI have a question:
- Why do we use "@ Override" annotation while writing this method? won't that method work without that annotation?
- I couldn't understand the use of "\"
Can someone please clarify me..
Sandhiya Ramesh
863 PointsIt is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes. The implementation in the subclass overrides (replaces) the implementation in the superclass by providing a method that has same name, same parameters or signature, and same return type as the method in the parent class. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed.
Admire Dhodho
5,411 PointsAdmire Dhodho
5,411 Pointsthanx sanders it worked
Jess Sanders
12,086 PointsJess Sanders
12,086 Pointsglad to help! select the answer as best answer, and you won't get any more unnecessary responses. :) Good luck as you continue learning Java!
Gonzalo Torres del Fierro
Courses Plus Student 16,751 PointsGonzalo Torres del Fierro
Courses Plus Student 16,751 PointsJess is correct....the rest of the code it is perfect...must run including the plus sign before the mAuthor