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 Organizing Data Splitting Strings

Chris Rubio
Chris Rubio
1,643 Points

How 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 ?

Daniel Nakonieczny
Daniel Nakonieczny
Courses Plus Student 54,657 Points

In 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.

class Product {
    public String manufacturer() {
        return "Apple";
    }
}

class iPhone extends Product {
    public String iPhoneName() {
        return "iPhone X";
    }
}

class AppleTV extends Product {
    public String appleTVName() {
        return "Apple TV 4K";
    }
}

public class BatchProcessingJob {
    public static void processProducts(Product[] products) {
        //Hmm, we only get Product[] here, but I'm sure I'm sending AppleTV[] to this method (see below)
        //let's get the first product from the list:
        Product p = products[0];
        p.manufacturer(); //this works!
        p.appleTVName(); //this fails!

        //we need to CAST the product to the correct type
        if (p instanceof AppleTV) { //doesn't hurt to check the type
            AppleTV atv = (AppleTV)p;
            atv.appleTVName(); //now we can call it!
        }
    }
}

AppleTV tv1 = new AppleTV();
AppleTV tv2 = new AppleTV();

AppleTV[] appleTVList = new AppleTV[]{tv1, tv2};

1 Answer

Daniel'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.