Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures Getting There Type Casting

Herman Vicens
Herman Vicens
12,540 Points

I don't know how to retrieve the mTitle from the getTitle() method

I've tried many things here and nothing seems to be working here. My research in the different support sites did not produced the examples required to solve this issue. I would appreciated if you tell me why BlogSpot.getTitle() is not an option in the return statement.

Do I need to instanciate BlogSpot? I know that obj is an instanceof BlogSpot.

thanks,

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) {
   if (obj instanceof String) {
      return (String) obj;  
   } else if (obj instanceof BlogPost) {
          return (String) BlogPost.getTitle();     
   } else {
    String result = "";
    return result;
   }
  }
}

6 Answers

Gabe Olesen
Gabe Olesen
1,606 Points

Woops!

I would put that to lack of coffee , my mistake. I forgot to close the scope and remove a parentheses:

public static String getTitleFromObject(Object obj) {
     String result = "";   
      if (obj instanceof String) {
         result = (String) obj; 
     } else if (obj instanceof BlogPost) {
         result = ((BlogPost) obj).getTitle();
          }
      return result; 
    }
Gabe Olesen
Gabe Olesen
1,606 Points

Hey Herman Vicens,

You're very close, for task 1 we want to cast result (String) to obj if it's in fact a String, so:

public static String getTitleFromObject(Object obj) {
     String result = "";   
      if (obj instanceof String) {
         result = (String) obj; 
  } else if (obj instanceof BlogPost) {
         result = ((BlogPost) obj).getTitle());
}
   return result; 

So you can see the String result is initialised and it's empty, the first if statement will return whatever argument passed into it is in fact a String hence the instanceof.

For the second if statement we're checking if the argument passed in is an instanceof BlogPost if it is we will return the result by calling obj.getTitle() as we've type cast down the hierarchy and obj is of type BlogPost.

Hope this helps!

Herman Vicens
Herman Vicens
12,540 Points

Thank you Gabe! I've tried that but it did not worked. Do you have an extra parenthesis in your response? It is complaining about not being able to convert BlogPost to a String and the carat sign is under the dot in obj.getTitle().

Herman Vicens
Herman Vicens
12,540 Points

You were right! I took the extra parenthesis and it worked!! Thanks a lot!!

Gabe Olesen
Gabe Olesen
1,606 Points

No problem!

Best of luck coding!

Herman Vicens
Herman Vicens
12,540 Points

Thanks. Go get some cofee!!