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 Type Casting

I'm stuck on the second part of the challenge. Where and how do you call the getTitle method?

Any help is appreciated.

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;
    }
}
TypeCastChecker.java
import com.example.BlogPost;

public class TypeCastChecker {
  /***************
  I have provided 2 hints for this challenge.
  Change `false` to `true` in one line below, then click the "Check work" button to see the hint.
  NOTE: You must set all the hints to false to complete the exercise.
  ****************/
  public static boolean HINT_1_ENABLED = false;
  public static boolean HINT_2_ENABLED = false;

  public static String getTitleFromObject(Object obj) {
    // Fix this result variable to be the correct string.
    if (obj instanceof String) {
      return (String) obj;
    }

    if (obj instanceof BlogPost) {

      obj = (BlogPost) obj;
      obj = BlogPost.getTitle();
      return obj;
    }
    String result = "";
    return result;
  }
}

2 Answers

Florian Tönjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tönjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hey magnusmartin,

your code:

  public static String getTitleFromObject(Object obj) {
    // First part omitted for clarity
    if (obj instanceof BlogPost) {

      obj = (BlogPost) obj;
      obj = BlogPost.getTitle();
      return obj;
    }
    String result = "";
    return result;
  }

You are first checking if 'obj' is a 'BlogPost', this is right. Then you are casting 'obj' to a 'BlogPost', this also works, but you are assigning the result again to the variable 'obj', which is defined in the parameter of the method as being of type 'Object'. This nullifies the type casting you have done because it becomes an 'Object' again, which does not "know" it has the method 'getTitle'. So you are not able to call this method on it.

The code doesn't even get this far at the moment because you are not calling 'obj.getTitle()' but 'BlogPost.getTitle()', so you are not calling the method of a 'BlogPost' instance but of the class 'BlogPost' itself and since the 'getTitle()' method is not 'static' it can only be called by an instance.

Here is a commented version of what it should look like:

  public static String getTitleFromObject(Object obj) {
    // First part omitted for clarity
    if (obj instanceof BlogPost) {

      BlogPost blogPost = (BlogPost) obj; // Assigning the result of the type casted obj, which is an instance of BlogPost, to  a variable of type BlogPost
      String title = blogPost.getTitle(); // Calling the getTitle() method of the BlogPost instance 'blogPost' and assigning it to a String variable
      return title;                       // Returning the String
    }
    String result = "";
    return result;
  }

Kind Regards, Florian

Thank you very much Florian for your clear explanation. :)