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 trial

Java Java Data Structures - Retired Getting There Override an inherited method

Overriding 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;

com/example/BlogPost.java
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

You 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. :)

thanx sanders it worked

glad 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
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

Jess is correct....the rest of the code it is perfect...must run including the plus sign before the mAuthor

Joe Goodall
Joe Goodall
3,483 Points

Right after the constructor I added:

@Override

public String toString() {

return "BlogPost: \""+mTitle + "\" -@" + mAuthor;

}

Still not working? Anybody any idea why not?

That code worked fine when I tried it just now.

Gonzalo Torres del Fierro
Gonzalo Torres del Fierro
Courses Plus Student 16,751 Points

i 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
PLUS
Schwartz Prince
Courses Plus Student 2,132 Points

I have a question:

  1. Why do we use "@ Override" annotation while writing this method? won't that method work without that annotation?
  2. I couldn't understand the use of "\"

Can someone please clarify me..

It 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.