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 trialHamadi Hachemi
Courses Plus Student 7,005 PointsStuck 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
Treehouse TeacherIn 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.
Jose Aguirre
14,866 Pointspart 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
25,764 PointsBelow 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
3,230 Points<p>
if (obj instanceof BlogPost) {
BlogPost post = (BlogPost) obj;
return post.getTitle();
}
</p>
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
}
Efe Özelçi
3,776 PointsI want to exact solution. Whatever I did, I never achieved the result.. Please give me the exact code asap. Thank you.
Efe Özelçi
3,776 PointsI 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;
Hamadi Hachemi
Courses Plus Student 7,005 PointsThank you sir, it's working now :)
Hamadi Hachemi
Courses Plus Student 7,005 PointsHamadi Hachemi
Courses Plus Student 7,005 Pointswell, as i understand it, it should be like this !
just small problem on how to return the result of getTitle method !
Craig Dennis
Treehouse TeacherCraig Dennis
Treehouse TeacherWhat if you created a new
BlogPost
variable from that casting of the object? Then you'd havegetTitle
available.Desmond Wong Jun Wei
Courses Plus Student 287 PointsDesmond Wong Jun Wei
Courses Plus Student 287 PointsThis is my attempt, and I can't understand any further
Simon Keating
10,368 PointsSimon Keating
10,368 PointsI'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...