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

Why would you cast a com.example.BlogPost to BlogPost? Isn't it already a BlogPost??

I understand checking and casting the argument obj to a String, because it could be anything. But I'm very confused by the way the instructions are presented. It says to cast an incoming com.example.BlogPost into a BlogPost... Why did he write it that way (the full package name)? How is com.example.BlogPost different from BlogPost?

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;

    return result;
  }
}

1 Answer

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

HI Ryan

I will try to answer your question if you may. In the first challenge it said that it is possible that the system will pass a String OR BlogPost from com.example.BlogPost through variable argument obj. Meanwhile obj is still declared as Object. Therefore you need to change the obj variable type from Object to String or BlogPost to access the features of each type. Just for your information in the second challenge they will still check if you still confirming a String casting type or not. Therefore if your (String)obj casting is not working it will said 1st challenge is no longer working or something...

In the second challenge you need to access the feature getTitle() which is in BlogPost type of object right? To do this you need to cast the object type BlogPost to variable obj. Then the compiler will then know you can access a method called getTitle(). This is the code:

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){
      BlogPost blogPost = (BlogPost)obj;
      result = blogPost.getTitle();
    }
    return result;
  }

As you can see I store the object BlogPost on a new object called blogPost. This is because the variable called result has already declared as String, thus unable to store object of BlogPost type. In order to store the obj object to the blogPost variable I need to tell the compiler that I guarantee this obj is a BlogPost type. Because if you fail to guarantee it an insert another type of object such as Integer or boolean it will invoke runtime error. Therefore we check it first using if(obj instanceof BlogPost).

As for why it was written as com.example.BlogPost I guess it just to make sure that the BlogPost is the object form folder com/example/BlogPost. That is why it needed to be imported as you can see on top of the challenge code there is import com.example.BlogPost

I hope this will help you a little...

foxtails
foxtails
3,655 Points

Thank You! This is a very good answer!