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

Leen Leenaerts
Leen Leenaerts
1,096 Points

What am I doing wrong?

https://teamtreehouse.com/library/java-data-structures/getting-there/type-casting-2

I cannot open the hints since it says task 1 doesn't work anymore after I didn't change anything at all.

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 result variable to be the correct string.
    String result = "";
    if (obj instanceof String) {
      result = (String) obj;
    }
     if (obj instanceof BlogPost) {

      result = (BlogPost) obj.getTitle();
    }

    return result;
  }
}

2 Answers

For this to work, you must first create a new BlopPost object and TypeCast the obj into it. So if obj instanceof BlopPost, then you would want to create a new BlogPost variable using the obj variable that is taken by the method.

BlogPost blogpost = (BlogPost) obj;

The reason you have to do this first is because obj is declared as an Object. Object does not have the method getTitle() so it doesn't work, the computer doesn't know what to do so the compiler breaks. We have to change the Object class into a BlogPost class in order to access the methods that are in the BlogPost class.

I hope this is helpful. Please let me know if you have any questions. Happy coding!

Leen Leenaerts
Leen Leenaerts
1,096 Points

Thanks a lot! It worked! So the only way to do get the String out of it is to create a new object BlogPost? There is no way to let the getter work on a TypeCasted object?

Well in a sense you are doing exactly that. We have to TypeCast the object variable into the BlogPost variable so we can utilize the methods inside the BlogPost class. The Object class does not have a method to get the Title, so we have to TypeCast the Object and put it into a new BlogPost variable, such as the line of code in my previous answer. This way, we now have access to all the methods we created in the BlogPost class and can utilize the method getTitle() to get the String that we want.