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

Jacob Gonzales
Jacob Gonzales
1,837 Points

I don't think I completely understand the problem?

It ask for me to return the proper string. Also, it says to check the type being put in the parameters, but the parameter type is Object? So wouldn't it give an error if you tried to put anything else into it? I'm just confused.

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 = true;
  public static boolean HINT_2_ENABLED = false;

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

  }
}

2 Answers

michaelcodes
michaelcodes
5,604 Points

Hi there! No problem I am glad I could help! For the second part of this challenge you are extremely close with:

if ( obj instanceof BlogPost) { 
result = (BlogPost) obj.getTitle(); 
} 

The problem here is that you cannot call the .getTitle method directly from the generic object, You can only call the method once it is typecasted as a blogpost. We can fix this by adding a set of parenthesis around our object, so that it is typecasted first before the method is called:

if ( obj instanceof BlogPost) { 
result = ((BlogPost) obj).getTitle(); 
} 

This will cast our object as a blogpost first, then access the getTitle method and store it in our result variable.

To further demonstrate this concept I will show you a way we could do this in 2 steps:

if ( obj instanceof BlogPost) { 
BlogPost bp = (BlogPost) obj;
result = bp.getTitle(); 
} 

This does the same thing as the first example, but in 2 steps and using an extra variable.

Hope this helps and happy coding !

michaelcodes
michaelcodes
5,604 Points

Hi there, so first I want to say we must be careful with java capitalization because it is case-sensitive. instanceOf is actually all lowercase (instanceof).

To answer your question, since object is the parent of all in java, every object is a child of it. This means that object is a very broad type. The method could be taking many different objects (2 in this case). We can type cast them appropriately and then do as we need.

Here is what the typecasting of object would look like, if it contains a String and not a BlogPost object.

    String result = "";
    if(obj instanceof String) {
      result = (String) obj;
    }

If you have anymore questions let me know!

happy coding!

Jacob Gonzales
Jacob Gonzales
1,837 Points

Sorry I'm responding so late, but on the second part when I check to see if it is a blog post I tried if ( obj instanceof BlogPost) { result = (BlogPost) obj.getTitle(); } but I keep getting a syntax error? Thanks so much for the help by the way!