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

Stuck at typecasting in Data Structure

Hello everyone, I can't understand the second challenge at all, can anyone help me !.

Now make sure that if a com.example.BlogPost is passed in for obj that you then cast it to a BlogPost. Return the results of the getTitle method on the newly type-casted BlogPost instance.

thanks in advance.

7 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

In pseudo-code:

// if obj is an instance of BlogPost
//      cast obj to Blogpost
//      return the result of the getTitle method on BlogPost

That help? If not.. share your attempt and I can help try to clear things up.

well, as i understand it, it should be like this !

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

just small problem on how to return the result of getTitle method !

Craig Dennis
Craig Dennis
Treehouse Teacher

What if you created a new BlogPost variable from that casting of the object? Then you'd have getTitle available.

This is my attempt, and I can't understand any further

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)
      return (String) obj;

    if(obj instanceof BlogPost)
      return (BlogPost) obj;

    return result;
  }
}```
Simon Keating
Simon Keating
10,368 Points

I'm a little confused about the pseudo-code above :)

If an object is already an instance of BlogPost, why are we then casting the object to BlogPost? I don't really understand the need/benefit...

Jose Aguirre
Jose Aguirre
14,866 Points

part 2 public static String getTitleFromObject(Object obj) { // Fix this result variable to be the correct string.

String result = "";
if(obj instanceof String){
  result = (String) obj;    
} else if(obj instanceof com.example.BlogPost){ //check if obj is an com.example.blogpost
  BlogPost bp = (BlogPost) obj;   //if true down cast obj to blogpost
  return bp.getTitle();  // return the instance's (bp) title
}
return result;

} }

Nelson Fleig
Nelson Fleig
25,764 Points

Below is the cleanest solution I could come up with. This way you are "fixing" the result variable as per the instructions. Please note you declare and return the "result" only once.

    String result = "";
    if (obj instanceof String) {
      result = (String) obj;
    }
    if (obj instanceof BlogPost) {
      BlogPost bp = (BlogPost) obj;
      result = bp.getTitle();    
    }
    return result;```

I think it could have been made clearer in the video that you need to create a variable (i.e. BlogPost bp) with the type of parameter (here a BlogPost object) you are expecting the getTitleFromObject method to recieve. The method getTitle returns a String so there are no type conflicts with the string variable "result".
Elis Amet
Elis Amet
3,230 Points
<p>
if (obj instanceof BlogPost) {
   BlogPost post = (BlogPost) obj;
   return post.getTitle();
}
</p>
Jose Aguirre
Jose Aguirre
14,866 Points

// here is the way i did it gals and guys //The method getTitleFromObject will be called and passed a String and/or a com.example.BlogPost. // For this first task, return the object obj type casted as a String if it is in fact a String.

// PART 1: //in method

//var declaration    
String result = "";
if(obj instanceof String){         // check if obj is String
  result = (String) obj;    //if true down cast obj to string
} 
return result;  // return result

}

I want to exact solution. Whatever I did, I never achieved the result.. Please give me the exact code asap. Thank you.

I found the answer finally. I'll write to there for some students who do not understand other answers like me.

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

return result;

Thank you sir, it's working now :)