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 Getting There Type Casting

Chris Rubio
Chris Rubio
1,643 Points

MInute 3:12 ?????

Im confused on minute 3:12, craig says something about lets say a person thinks this is a "treet" and he casts everything ? idk this example is so confusing ? if someone can , please explain this example he talks about and if you can provide me with a different example, because i have been on this lesson for HOURSSS and i am frusturated at this point

3 Answers

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi Chris

What Craig's try to describe there is if there is two objects in an array (or let's say a list) the first object is a Treet and the second object is a String but the person responsible to handle those object does not know for sure of it. Then he tries to cast the first object by asking the mDescription which is one of the attribute of a Treet object. The first object answer it correctly and that makes the person assume that the second object is also a Treet (in fact in reality it was a String). Then that same person (assuming the second object is also a Treet) tries to do the same thing as he/she doe to the first object: trying to asks to get mDescription attribute. Since object type String does not have that attribute (mDescription) the Java gets confused and throws a Runtime Exception (worst kind of error) to the person saying "What are you talking about? This is a String, it cannot gives you that kind of information because String does not have them in the first place.

To be save let me give you another simple real life simple example: Let's just say I am that person. I am a foreign tourist from remote 3rd world country visiting United States. Being less informed I received a guide book that have information about two food delivery service, one is let's say from MacDonald, and the second is from Domino's. Once again I am less informed I do not know what Mac Donald and Domino's are (Weird I know but let's just say I am ignorant).

I am hungry and that makes me so ignorant that I start assuming since I am in United States when we talked about food it means Cheese Burger, all food store and restaurant in US must have Cheese Burger. So I call the Mac Donald since its' number is on the top of the list. They answered "okay I will send you Cheese Burger". I am so happy that my uneducated guess is correct. Then it got me thinking: "hey I am so hungry... let's order one more Cheese Burger, but this time from another restaurant called "Domino's" and let's compared the taste".

Then assuming all restaurants in US have Cheese Burger as menu I called Domino's and asked for a Cheese Burger. The operator gets confused and saying "Sorry sir, we are pizza restaurant, we do not serve Cheese Burger". I was so ashamed of my careless assumption that it creates an error which is calling something that does not served in Domino's. I should done better than this, I can search first to know what kind of menus Mac Donald and Domino's have before making the call and make the operator confused with my uneducated requests.

This step if making sure is similar with the use of instance of that Craig's use to make sure if object one is a String. It answer false. Then he asked again is object one instance of Treet the answer is True. It is like asking if Cheese Burger available in Mac Donald? The answer is True, I can proceed to order it from Mac Donald. In contrary is Cheese Burger available in Domino's? The answer is false please do not embarrass yourself by ordering it through phone and making the operator confused.

I hope this can help you a little. Keep on going. Through the course and exercises you will get the answer of your questions. You can always seek for answer from the community also.

Disclaimer: I do not really know for sure if Domino's does not serve Cheese Burger I only assume that since it was a Pizza restaurant it does not so I used it as example since its' popularity as pizza restaurant. I apologize if I was wrong

Chris Rubio
Chris Rubio
1,643 Points

Wow i have been studying for hours and your response is the bestttttt .. thank you !

Chris Rubio
Chris Rubio
1,643 Points

Do you mind explaining the first minutes of te video (upcasting and downcasting) an example like this previous one would be great , pleaseeeeee and thank you

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Well Chris....

The easiest example back by using Mac Donald and Domino's as example. If you classified Mac Donald and Domino's as restaurant it works just fine right? I mean they both serves food just like any other restaurant for example Taco Bell, Wendy's, or even like fancy restaurants like Le Bernardin. This is upcasting.

However, the main disadvantage of doing upcasting is you cannot access certain feature like order Cheese Burger. If you order Cheese Burger at Domino's or taco Bell they will laugh at you. And the chef at Le Bernardin will say "Do you prefer Wagyu beef to come with your cheese?"

So you cannot just order Cheese Burger at any restaurant. You need to classify down to the restaurant that actually serve burgers (many kind of burgers right?) then you got bigger chances to get the Cheese Burger right? Go to Burger King, Wendy's, instead of McD for instance. You can tell they serve burger since it feature burger in their logos. Then you classify Burger King and Wendy's as burger restaurant and assumes it has some kind of Cheese Burger. This is downcasting.

I know this is a long shot. Many concepts need to be learned from this casting course. But this is just for starter, just to give you some perspective how to recognize upcasting and downcasting. As you move forward with your lesson you will learn much more and becoming an expert. Casting is an important concept in Object oriented programming. There are more to it than the example I just gave you. It is important to master it. Use my example as starting point of upcasting and downcasting concept but do not forget to keep moving forward and study more. I also still struggling to study Java but I think the main key concept is to move forward and there will be times when our biggest trouble of understanding coding terms and concepts in the past enlighten by future lessons. At that point you will look back at this moment and smile as you are ready to help others that have similar difficulties as you are right now.

Sorry for the long too philosophical blabbering, I hope this can help a little

Teacher Russell
Teacher Russell
16,873 Points

Very good explanations, and very entertaining as well. Also, give them time, and one day Dominoes will server full hamburgers as a topping on pizza. At least here in the US. I wish we could have spent more time on casting here. It does seem like a thick topic. What other resources would you recommend on this subject, aside from the links provided here? Is there any Java teacher on youtube you might recommend. No one seems to go very deep into subjects.

Chris Rubio
Chris Rubio
1,643 Points

you have been very great, thank you so much :)