Generics / Wildcards & Nesting
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
Earlier you saw how to use the question mark character to represent any class and you saw how to use the extends keyword to limit the class to a specific subclass and implementers of a specific interfaces. Now I want to show you the results of using them together. This is the same stash class that we looked at earlier, but this one is used a bit differently. This is an attempt to allow generics to use polymorphism, but it only partially works. Notice this reference for a stash object named number stash is declared with a question mark for the type, but it also binds the unknown type to be something that extends the number class. This is the instantiation of an object that meets the requirement, but we have to start with it in a reference to its own type named integer stash because we couldn't write to it if we were to put it into number stash. It's because of that lack of inheritance thing I talked about in the previous lesson. Anyway, with the stash object being a reference of the right type, integer objects can be added to it freely. Now that we're not going to write to it anymore, we can copy the address of the integer stash object into the generalized number stash object. We can't write to it when it's in this form, but we can read from it as long as we are not specific about the type of number that we read. Every number object has a two-string method and it can be called. Here is almost the same thing done again, this time with a double, which is also a subclass of number. The only difference is that the returned object is cast to its class instead of being left as its super class. So far we have looked at the lower-bounds definition, which has the form of using the extends keyword like this. This means that the objects must be a number object or an object of one of its subclasses, such as integer, double, sort, byte and so on, but it is also possible to have upper bounds like this. This form limits the choices to the number class or the objects class, which is its only super class. Here's an example of using two type declarations in the generic definition. You can include as many as you like this way. Just separate the names with commas. Here are two simple wrappers. The pair class contain two objects and the stash class can contain one. Here's the example of the two of them used together. This is the declaration of a reference named SP that can contain the address of a stash object and the stash object can contain a pair object. The pair object can contain a string object and a long object. This statement constructs the actual object. These final two statements build the string object and long object and put them in a pair wrapper and then puts the pair wrapper in the stash wrapper. To demonstrate that it works, these final two statements pull the objects out of the wrappers and display them. By the way, this is an annotation. We'll be talking more about them later. The purpose of the annotation is to suppress a warning message about something being unchecked. If you use generics much, you'll often get these false warning messages. The usually don't mean anything. The class compiles just fine, but they are a nuisance. There are lots of different types of warning messages and you can find that you may want to suppress a lot of them. When this program runs, it looks like this. That covers the basics of generics. Beginning with the next lesson, we'll be looking into Java's collection Framework and several of them use generics.
Tutorial Information
| Course: | Java 6 |
| Author: | Arthur Griffith |
| SKU: | 33858 |
| ISBN: | 1-934743-59-3 |
| Release Date: | 2008-02-29 |
| Duration: | 7 hrs / 92 lessons |
| Work Files: |
Yes |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 