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 going to explore lower bounded wildcards. These are denoted as '<? super T>'. Can anyone tell me what this might mean?
I think it means we can put an object of type T or its supertypes into a collection?
Exactly! Lower bounded wildcards allow you to add elements of type T and its supertypes into collections. This is particularly useful for lists or sets where you want to maintain a flexible data structure.
Can you give us an example of where this would be useful?
Certainly! Imagine you have a method that needs to accept a list of numbers. Using List<? super Integer> means that you can add Integers and any type that is a supertype of Integer, like Number.
Let’s further explore practical examples. How might we declare a method that accepts lower bounded wildcards?
Maybe something like public void addNumbers(List<? super Integer> list)?
Great! This method can now add Integers into the list while allowing any superclass of Integer as well. What happens if we try to read from this list?
We wouldn't know the exact type, so we can't safely read the elements, right?
Exactly! This limitation is important when dealing with lower bounded wildcards; they are primarily for consumption.
Let's compare lower bounded wildcards with upper bounded wildcards. What’s the difference between these two?
Upper bounded wildcards allow us to read objects of type T or its subtypes, right?
Correct! While lower bounded wildcards are more about adding into collections. Remember the acronym PECS: Producer Extends, Consumer Super.
That’s a great way to remember how to use them!
Exactly! It's essential to grasp this distinction to use wildcards effectively in generics.
Can anyone suggest a real-world application where we might use lower bounded wildcards?
How about in a scenario where we have multiple shapes, and we want to add them to a list or collection?
Excellent example! If we had a supertype like Shape, we could use List<? super Circle> to add Circles and other shapes that extend from Shape.
So it’s about achieving flexibility while maintaining type safety?
Exactly. That’s the beauty of using generics and wildcards together!
Let’s summarize what we've learned about lower bounded wildcards. What’s the main takeaway?
They're useful for adding elements of a specified type and all its supertypes.
Correct! And remember that while they allow data addition, they limit data reading. What’s the helpful acronym we discussed?
PECS—Producer Extends, Consumer Super!
Great job! Keep practicing these concepts, as they’re foundational for working with generics in Java.
If you want to sow, use lower bound, super types will be found.
Imagine a library that accepts books of various genres. The library allows any book or its superclass genre, enabling a wider range of additions.
Plugins of Lower Focus on Adding Easily to SuperTypes.
Lower Adds Super Types (LAST): Use lower bounded wildcards for adding to lists.
Term
Lower Bounded Wildcards
Click to flip
Term
PECS Rule
Click to flip
Wildcards in Java generics denoted as <? super T> allowing for writing items of type T and its supertypes.
A higher-level class from which other classes (subtypes) are derived.
A feature that ensures that the program only interacts with compatible types, reducing runtime errors.
An acronym that stands for Producer Extends, Consumer Super, guiding the use of wildcards in generics.
Lower bounded wildcards in Java generics allow for writing items of a specified type or its supertypes.
Lower bounded wildcards enable developers to write objects of a specific type as well as any of its supertypes into a collection. This flexibility is crucial for scenarios requiring generalized support for various object types while maintaining type safety.
Lower bounded wildcards, denoted as <? super T>, are a feature of Java's generics that allow collections to accept a specific type T and any of its supertypes. This is useful when you're looking to add items to a collection, ensuring that the collection can contain items that belong to a type hierarchy while adhering to the principles of type safety.
? super T indicates that the collection can accept T as well as anything that is a supertype of T.List<? super Integer> that can accept Integers as well as any of its supertypes such as Number or Object.By utilizing lower bounded wildcards, developers gain enhanced flexibility in managing collections, promoting both code reusability and type safety.