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

Allen C
Allen C
1,448 Points

Type Casting - Please help!

I'm new to java programming and Type Casting is really messing me up. I understand Inheritance but can't seem to grasp on how Type Casting works. The exercise after this lesson was especially confusing for me.

Does anyone here have any tips on how to understand this concept and get a better grasp on it for a beginner like myself?

The challenge that I needed to do research on was: 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.

If anyone has the time, could someone please explain what this question really means? What does it mean when it says "a com.example.BlogPost is passed in for obj ".

what is the type-casted BlogPost instance?

Thanks in advance.

Hey there..

Any new programming language has it's Ah-ha moments and Java surely comes packed with a lot of concepts. It's great that you are down with Inheritance and fret not, it's only a matter of time before Type Casting is a piece of cake too.

To answer the first part of your question -

  1. The best source to enhance your knowledge of Java concepts would have to be the Oracle Javadocs and here is the link to the Inheritance and Casting topic

  2. Futhermore, there are multiple online programming forums such as Stack Overflow and Code Ranch where the community discusses common or specific coding related issues. And the likelihood of your query being asked previously and already answered is really high. You may check out this link if it helps your case or dig deeper if you aren't satisfied by the top answer.

  3. You might even want to get your hands on a good java programming book to complement your journey down the Treehouse stack or perhaps at a later time. Simply because it's always helpful to have a couple of good sources at hand while learning any new subject. If you need tips on the best books, you will find them on the forums ;)

Alright, now to answer the research question, just gonna take a stab at it. The way I understand it , it seems like the question is prompting at something on these lines, to quote the Javadoc on Inheritance and Casting

" You can make a logical test as to the type of a particular object using the instanceof operator. This can save you from a runtime error owing to an improper cast. For example:

if (obj instanceof MountainBike) {
    MountainBike myBike = (MountainBike)obj;
}

Here the instanceof operator verifies that obj refers to a MountainBike so that we can make the cast with knowledge that there will be no runtime exception thrown. "

So in the Treehouse example, it may look something on the lines of -

String title = "";
if (obj instanceof BlogPost ) {
    BlogPost blogpost = (BlogPost)obj;
    title = blogpost.getTitle();
}
return title;

Hope this helps.