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

Paramvir Singh
Paramvir Singh
1,517 Points

Stuck at part 2

unable to do part 2

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 r = null;

    if (obj instanceof String ) {
      r = (String) obj;
    }

    if (obj instanceof BlogPost) {

      obj = new Object();
          obj = (BlogPost)obj;
      r = obj.getTitle();
    }

    return r;
  }
}

1 Answer

andren
andren
28,526 Points

You are somewhat on the right track but there are some issues.

With this line:

obj = new Object();

You override the passed in obj so that the BlogPost that was passed into the method is replaced with a new empty Object.

Also this line:

obj = (BlogPost)obj;

Does not actually end up doing much of anything. Since obj is an Object variable Java automatically casts anything assigned to it to an Object. Which means that you are essentially converting the obj from Object to BlogPost manually, then java converts the BlogPost back to Object automatically. Which ends up cancelling each other out, resulting in no change having occurred.

The simplest way of solving this task is to create a new BlogPost variable, and then assign to it obj cast as BlogPost. And then call the getTitle method from that new variable. Like this

if (obj instanceof BlogPost) {
  BlogPost blogPost = (BlogPost)obj; // Create BlogPost variable and assign `(BlogPost)obj` 
  r = blogPost.getTitle(); // Call `getTitle` on that new variable
}

Also I noticed that you changed the name of the String variable that is returned in this task from result to r, and changed it's initial assigned from "" to null. While that does not appear to cause any issues in this particular task I would advise against doing so in the future on other challenges. Challenges often rely on variables having certain names and values in order to test your code, so changing them when you are not prompted to do so can often break the challenge checker for the task.

Paramvir Singh
Paramvir Singh
1,517 Points

Hi,

I got what you are trying to say. Just to be clear, if I downcast an Object variable(like I was doing with obj) and assign it to an Object variable(which was obj in this case) it's of no use as they end up canceling each other, right?

And thanks for advising me on not changing the names of the variable. Once again thanks a lot.