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 trialChris Rubio
1,643 PointsHow vital is me knowing casting ?? can i move on ?
Casting for me was a complete failure, i got an idea on what to do , but did not understand WHY we use casting. I spent 2 whole dAys ( 10 hours total) trying to understand it and still no luck ,is it safe for me to move on ?? or am i going to be even moreeeeeeee confused if i dont fully grasp casting ??
(random question) how do i get more treehouse user responses ? i usually only get 1 person to respond to my posts ? i would like to get different perspectives thats why i ask ?
1 Answer
liamthornback2
9,618 PointsDaniel's answer was great, but I think there is some more I could add to this question:
Firstly, having the kind of trouble with type casting that you are having might be a sign that you have trouble understanding object-oriented programming and types in general. If you understand inheritance, type casting shouldn't be a very difficult thing to wrap your head around. If that is the case I recommend you go on YouTube and you'll find a lot of great resources for learning the basics of object oriented programming if you look hard enough.
If that is not the case and you're just having trouble with type casting, I recommend you just push on for now but remember to come back to it later when you're fresh. Don't give up on a topic (even a non-essential one) because you feel like you don't get it at first.
As for getting more than one response to your questions, there is not a lot you can do. There is a lot Treehouse could do to improve their community system.
Daniel Nakonieczny
Courses Plus Student 54,657 PointsDaniel Nakonieczny
Courses Plus Student 54,657 PointsIn my programming career I've used casting from time to time. It's not used every day, but sometimes it helps.
Let me try to explain this in a different way:
Now, let's say that you have a class that processes Products, and there's a method there (processProducts()) with a signature that you cannot modify, meaning the method needs to accept an array of Products, and you cannot modify it to accept an array of AppleTV.
when you implement that method, it gives you a list of Product type inside it. Even if you KNOW that you are sending a list of AppleTV, and the code will compile because AppleTV inherits from Product, inside that method you will only have a list of Product instances, so you can only call manufacturer() on them.
What if you NEED to call appleTVName() or iPhoneName() on them, but calling those methods on Product gives you an error? You cast those instances to the type you need
See my code below. Keep in mind this is not the best optimized code, but it's here to explain the concept.