• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Using ? super in generics – Java 5.0

September 18, 2008 //  by Krishna Srinivasan//  Leave a Comment

As we know we can not add any value except null to collection if we use ? extends syntax. What if we want to add value to the collection, then we have to use ? super syntax. The code looks like this.

also read:

  • New Features in Java 5.0
  • Generics in Java 5.0<
  • Annotations in Java 5.0
List<? super Dog> list1 = new ArrayList<dog>();

before discussing why it is possible to add, using ? super syntax, let us see what can be assignable to list1 in above code.? super Dog means, the collection using this syntax can take either collection of same generic type or super generic type.

List<? super Dog> list1 = new ArrayList<dog>();
List<? super Dog> list2 = new ArrayList<animal>();
List<? super Dog> list3 = new ArrayList<object>();

above three line will compile but not below line

List<? super Animal> list4 = new ArrayList<dog>();

And now let us see what we can add to collection, if we use ? super syntax. we can add same generic type or sub type of the generic type used in the declaration.

so we can add Animal or Cat or Dog to below collection

List<? super Animal> list = new ArrayList<animal>();

Now the question is why we are allowed to add values to collection if we use ? super, which is not possible if we use ? extend ????????????.
the reason is, if we use ? super in collection declaration , the reference can point to same generic type of collection or super generic type of collection. so if we add Animal or subtype of Animal to ? super Animal, the added value(Animal or subtype of it) will pass IS-A test with all types, which are super to Animal.
For example Dog will pass IS-A test with Animal and Object. Below code can explain everything in detail. assume class MyDog extends Dog.

List<? super Dog> list = new ArrayList<dog>();
list.add(new Dog());
list.add(new MyDog());
list = new ArrayList<animal>();
list.add(new Dog());
list.add(new MyDog());
list = new ArrayList<object>();
list.add(new Dog());
list.add(new MyDog());

Now how we can iterate this collection using enhanced for loop. Think about it for a minute, when we are iterating the collection, in for loop reference has to point to an object which is super to Dog so it can be Animal or Object.So we need to use Object because it can hold any other object.

for(Object obj : list){
System.out.println(obj.toString());
}

will produce below output.

Dog MyDog

Category: JavaTag: Java 5.0

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Generic Methods in Java 5.0
Next Post: How to use ? extends in Generics? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact