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

Alex Kasper
Alex Kasper
1,896 Points

Java Type Casting how do I use the getTitle() method of BlogPost after the instanceof method?

I've tried to solve this exercise in multiple ways after reading various answers, but still can't seem to really grasp the answer.

I understand that the:

if (obj instanceof BlogPost)

is checking if the obj is an instance of BlogPost object. I don't understand why it won't use the getTitle() method of post. My understanding is that something is going wrong with my:

post = (BlogPost) obj;

I created the post object of parent BlogPost prior to the statement, but why won't it accept the getTitle() method? Am I not type casting this correctly? I believe that the getTitle() method is creating the string that we're looking for. I can only imagine that I'm not linking the objects together correct.

Also, do I need to create the post object prior to the if statement? I saw other people's answers and they seemed to be able to do this. Every time I have tried I that the compiler couldn't find the symbol.

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 = "";
    BlogPost post;

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

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

4 Answers

Two hints:

1) your first "return" is not in a if/else-statement so the program will finish with certainty on this line. In no case it will be possible to execute the lines below... so this is bad. You could then think, that you should put the return in one of the if-statements, but the problem is that the compiler will recognize, that in some cases (of a wrong input for example) no return statement will be executed. You can therefore save what you need in a variable, and give it back with a single return statement

2) try to get used to use "else if" instead of a multiple "if"-statements, when you know, that both can't happen at the same time. It makes the code clearer and you can better analyse what will happen and if things are exclusive or not.

Try it out on your own fist, but if it is not clear enough, here is what I mean:

  public static String getTitleFromObject(Object obj) {
    // Fix this result variable to be the correct string.
    String result = "";
    BlogPost post;

    if (obj instanceof String) {
      result = (String) obj; 
    }else if (obj instanceof BlogPost) {
      post = (BlogPost) obj;
      result =  post.getTitle();
    }
    return result;

  }
Alex Kasper
Alex Kasper
1,896 Points

Thanks a bunch! I did not realize that about the return statement. Realizing that now makes everything so much clearer. Also the else if statement.

You're welcome. I'm glad I could help