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

Dorukhan Arat
Dorukhan Arat
31,325 Points

I can't understand of second part of the exercise. Can you help me?

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

My first part of exercise looks like this.

1 Answer

Hi there,

The obj is either a string or a BlogPost. So, you've dealt with the String eventuality so now you need to handle the BlogPost eventuality. Remember, the method returns a String.

So, in the else clause, cast obj to BlogPost. Then surround that all in brackets, and call getTitle() on that using dot notation.

Either assign that into result or just return it directly.

Let me know how you get on.

Steve.