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

casting as BlogPost and returning the title

I'm going to have to sleep on this one - I think I see that we have to typecast the obj into a more specific type, but we need to return a String title (or result) right - does the obj, once typecast into a BlogPost, have an author, title, description and so on?

Well the below doesn't work.

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

1 Answer

Hi there,

Have a look through this post as I think it'll help you fix that.

Steve.

Thank you