Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
Earlier in this video course when I was describing the use of primitive variables, I described the process of conversion as either upward or downward. I described the conversion of an integer say to a float or double as an upward conversion and said that this sort of conversion is implicit, automatic, and safe, but if you try to go in the other direction converting downward where you take a very specific value and try to squeeze it into a less specific value that's where the conversion can lose data, and so you have to be explicit in your syntax.
This same thing is true of complex objects that have an inheritance relationship, when you use an object that's a subclass and you treat it as its superclass, we don't call that converting we call it casting, but we still use the same directions, the specific version being used as the more general version is called upward casting and taking an object that's been seen as the superclass and casting it downward to the specific object is called down casting. And just as with primitive variables you have to be specific.
Let's take an example, I'm working in a version of my application that's called casting objects. In this version of the application I removed a little bit of code, but I still have a declaration of an olive variable and then I'm instantiating that variable three times, twice as a Kalamata and once as a Liguirian, I'm then adding those objects to an array list that explicitly is declared as only containing instances of the olive class. I'm allowed to pass in the subclass, because that's an upward conversion and it's implicit and it's safe.
But now I'll show you what happens when you try to recast downward. I'll go down to the bottom of the code and I'm going to get a reference to the first item in that array list. I know that it's an instance of Kalamata class, because that's how I declared it up here. So I'll start off below all the other code and put in a data type of Kalamata and I'll name the variable olive1. And then I'll get a reference to that object using the syntax olives.get and I'll pass in a value of 0 for the first item in the array list.
Now when I save my changes and build the project I get this, type mismatch cannot convert from Olive to Kalamata, it's a downward casting that I'm trying to do. Even though I know it's a Kalamata olive, I created it as a Kalamata olive, the compiler and the JVM at this point will think it's simply an olive. So I have to tell the compiler its okay, it's a Kalamata olive. Here is how you do it, place the cursor before the code that's returning the reference and then declare the native type of the object wrapped in parentheses.
I'm telling the compiler and getting the reference to this object, but I want you to treat it as the subclass, I'll save the changes, the error goes away and all I have left now is a warning saying the value of that variable isn't yet used. So let's go ahead and add a little bit specific functionality to Kalamata, that's not available in all other olives. I hold down the Ctrl key on Windows or Command on Mac and click the name of the class Kalamata and that takes me to the declaration of the class.
I'll add a new public method into the class after the override of the crush method. It'll be a public method that returns a String and it will be named getOrigin and I'll return a value simply of "Greece" because that where Kalamata's come from. I'll save my changes and go back to the main class and then I'll output the return value from that method using a System.out.println command and the output will look like this "Olive1 is from" and then I'll append to that the value of olive1.getOrigin.
I'll run the application and there is the result Olive 1 is from Greece. Now if I didn't have this casting syntax I wouldn't even be able to compile the application, but if I could I wouldn't be able to call that get origin method, the compiler would say, well you said that was an olive not a Kalamata and if it's not a Kalamata then it doesn't have to get origin method. So this is how you cast objects downward from the general type to their specific type.
As you continue using the inheritance model in Java you'll find that this casting becomes more and more important. The ability to cast upward and downward so that you can either use objects in their general form that is as their superclass or in their specific form their subclass.
There are currently no FAQs about Java Essential Training.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.