Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we're diving into upper bounded wildcards in Java. These are represented by <? extends T>, which means we can work with a specific type and all its subtypes. Can anyone tell me why we would need to use this?
I think it's to allow flexibility in our code? Like, we can accept different types.
Exactly! It allows the use of various subclasses while maintaining type safety. Now, let’s consider a real-world example. If we have a method that accepts List<? extends Number>, what kinds of lists do you think we can pass?
We could pass a list of integers or doubles, right?
Correct! This ensures that we can work with different number types without losing type safety.
Now that we know what upper bounded wildcards are, let’s discuss where we can practically apply them. Why do you think these are useful in a programming context?
They make it easier to write methods that operate on different types of objects without repeating code.
Great observation! For example, we might want to write a method to calculate the average of a list of numbers. With List<? extends Number>, we can support any list that contains Number subclasses.
How do we ensure we’re only working with numbers then?
That's the beauty of generics. The type system enforces this at compile-time, preventing us from adding non-numeric types.
Let’s test our understanding. Who can explain the difference between upper bounded and lower bounded wildcards?
Upper is <? extends T> for reading subtypes, and lower is <? super T> for writing super types?
Excellent! Upper bounded wildcards, as we discussed, are meant for reading, while lower bounded wildcards allow us to write to a collection. Can anyone give an example of a scenario using lower bounds?
Maybe if I want to add elements to a list for a specific class or its superclasses?
Exactly! By using lower bounds, we can write to a list that accepts not just a specific type but also its supertypes.
A wildcard that’s upper, it’s not a fluke, it helps you peek into subclass nooks.
Imagine a box with an 'Animal' label. You can put in any kind of animal, like cats or dogs. The box is cozy and ensures no unrelated entities sneak in.
To remember upper bounded wildcards: 'Flexible Upper Types (FUT)'.
Use UBC (Upper Bounded Wildcard) to remember: Uniqueness, Bound Constraint.
Term
What does `<? extends T>` signify?
Click to flip
Term
What is the purpose of upper bounded wildcards?
Click to flip
Upper bounded wildcards allow for the reading of items of a specific type or its subtypes in Java generics.
Upper bounded wildcards are used in Java generics to work with declarations that restrict the unknown types to a specific upper limit. This allows developers to read and manipulate objects of a specified type or any of its subclasses while ensuring type safety.
Upper bounded wildcards in Java generics are represented using the syntax <? extends T>, where T is a specific type. This allows the handling of types and their subtypes, enabling operations that require type safety without losing flexibility. Upper bounded wildcards ensure that the object being manipulated adheres to the specified hierarchy, facilitating operations such as reading while maintaining type constraints.
List<? extends Number>, it can accept List<Integer>, List<Double>, etc.Understanding the use of upper bounded wildcards is vital for developers seeking to create flexible yet type-safe methods and classes within the Java Collections Framework.