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

Timothy Williams
Timothy Williams
1,536 Points

I am calling obj.getTitle wrong... but ?

The problem is line 18. If I set it to return "abc", it will. But I get a compile error. Also: wow... this casting came from nowhere. Why would it be useful to cast up or down?

public static String getTitleFromObject(Object obj) {
    // Fix this return statement to be the correct string.
    if (obj instanceof String){
      return (String)obj;
     }
     else if (obj instanceof BlogPost) {
       return (String)obj.getTitle;
     }
    else {
      return "";
    }
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 return statement to be the correct string.
    if (obj instanceof String){
      return (String)obj;
     }
     else if (obj instanceof BlogPost) {
       return (String)obj.getTitle;
     }
    else {
      return "";
    }

  }
}

2 Answers

Hello,

On line 18 in TypeCastChecker.java, you have:

return (String)obj.getTitle;

You have a few errors with this line. First, you are casting obj.getTitle as a String. Instead, you already know that obj is a BlogPost so you can safely cast it to that. Second, you are trying to get the getTitle member of the class, which doesn't exist, you want to call the getTitle() function on the newly cast BlogPost. If you have any further questions feel free to ask.

Timothy Williams
Timothy Williams
1,536 Points

I am still not "getting" it. Hmmm... why does this not work? How else may I call the getTitle() function?

 public static String getTitleFromObject(Object obj) {
    // Fix this return statement to be the correct string.
    if (obj instanceof String){
      return (String)obj;
     }
     else if (obj instanceof BlogPost) {
       String temp= obj.getTitle();
       return temp;
     }
    else {
      return "";
    }
  }
}

In this case, some extra parenthesis or a temp variable might help as well.

With a temp variable, you could do:

BlogPost post = (BlogPost) obj;
//use post as a BlogPost and call BlogPost functions on it

or you could do

((BlogPost)obj).someBlogPostFunction();

The first way is what I'd recommend for working with to start at least, but you could end up seeing either way done when reading code.

Timothy Williams
Timothy Williams
1,536 Points

OK!!! Thanks JAMES! I GET it!! I had to cast as (BlogPost) even though it is an instanceof BlogPost because the type received by the function was Object and not BlogPost. Thank you!

public static String getTitleFromObject(Object obj) {
    // Fix this return statement to be the correct string.
    if (obj instanceof String){
      return (String)obj;
     }
     else if (obj instanceof BlogPost) {
       BlogPost temp = (BlogPost)obj;
       return temp.getTitle();
     }
    else {
      return "";
    }