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

Madeline Yao
seal-mask
.a{fill-rule:evenodd;}techdegree
Madeline Yao
Full Stack JavaScript Techdegree Student 9,611 Points

Challenge Task 2 for Type casting

I am trying to work on Challenge Task 2 for type casting and does anyone know how to write the statement that checks whether the com.example is passed in or not and also how to return the results?

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

Bryanna Obrien
PLUS
Bryanna Obrien
Courses Plus Student 4,700 Points

Since we're working with the BlogPost class and creating BlogPost objects, we want to see if the 'object' being passed into the method is an instance of BlogPost.

We can test for that by using an 'if' statement and testing if our object is an instanceOf BlogPost. If it is, we could create a new BlogPost and type cast our object to it.

We could then use our method to get the part of our new blog post object that we want, type casting the output of our method to a String before we put it into our result.

Something like this:

if (obj instanceOf BlogPost){
  BlogPost myBlogPost = (BlogPost) obj;
  result = (String) myBlogPost.getTitle();
}

return result;