Can someone please explain the 'super' keyword in Java like I'm eight years old?
Java generics super keyword - Stack Overflow
ELI5 - What is super keyword in java
Clarification on this(); and super();
Why is Super important in Java?
Mention any three usages of the Java super keyword.
What is the use of the super keyword in variables?
Videos
I'm currently taking a Java course at my school and I couldn't really find any simple answers online. If anyone could help I would really appreciate it.
The bounded wildcard in List<? super Number> can capture Number and any of its supertypes. Since Number extends Object implements Serializable, this means that the only types that are currently capture-convertible by List<? super Number> are:
List<Number>List<Object>List<Serializable>
Note that you can add(Integer.valueOf(0)) to any of the above types. however, you CAN'T add(new Object()) to a List<Number> or a List<Serializable>, since that violates the generic type safety rule.
Hence it is NOT true that you can add any supertype of Number to a List<? super Number>; that's simply not how bounded wildcard and capture conversion work. You don't declare a List<? super Number> because you may want to add an Object to it (you can't!); you do because you want to add Number objects to it (i.e. it's a "consumer" of Number), and simply a List<Number> is too restrictive.
References
- Angelika Langer's Generics FAQs
- What is a bounded wildcard?
- When would I use a wildcard parameterized type with a lower bound? ("When a concrete parameterized type would be too restrictive.")
- Why is there no lower bound for type parameters? ("Because it does not make sense.")
- JLS 5.1.10 Capture Conversion
See also
- Effective Java 2nd Edition, Item 28: Use bounded wildcards to increase API flexibility
- "PECS stands for producer-
extends, consumer-super
- "PECS stands for producer-
Related questions
- Too many to list, PECS,
new Integer(0)vsvalueOf, etc
For the first part List<Number> fits in List<? super Number> but you can't add an Object to a List<Number>. That's why you can't add an Object to List<? super Number>.
On the other hand you can add every subclass of Number (Number included) to your list.
For the second part, String is an Object, but String isn't a superclass of Number.
If it worked like this, as every class is a subclass of Object, super would have no meaning.
Let's see every possible cases with List<? super Number> :
- The passed list is a
List<Object>List<Object>will workObjectfits in<? super Number>- You can add any subtype of
Numberto aList<Object> - Even if you could also add
Stringin it the only thing you're sure of is that you can add any subclass ofNumber.
- The passed list is a
List<Number>:List<Number>will workNumberfits in<? super Number>- You can add any subtype of
Numberto aList<Number>
- The passed list is a
List<Integer>(or any subclass ofNumber):List<Integer>won't work- Integer is a subclass of
Numberso it is exactly what we want to avoid - Even if an
Integerfits in aNumberyou wouldn't be abble to add any subclass ofNumberin aList<Integer>(for example aFloat) superdoesn't mean a subclass.
- The passed list is a
List<String>(or any class not extendingNumbernor in the "super hierarchy" ofNumber(ie.NumberandObject) :List<String>won't workStringdoesn't fit inNumber"super hierarchy"- Even if
Stringfits inObject(which is a super class ofNumber) you woudln't be sure to be able to add aNumberto aListthat contain any subclass from one of the super classes ofNumber) superdoesn't mean any subclass of one of the super classes, it only means one of the super classes.
How does it work ?
You could say that as long as you can add any subclass of Number with your typed List, it respects the super keyword.