• 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)

How to use ? extends in Generics?

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

In generics as we know we can not assign subclass generic type to super class generic type, for example
[code lang=”java”] List<animal> list = new ArrayList<dog>();
[/code] will not compile even though Dog IS-AN Animal. the reason for this is already explained in generics basics section.
How can we solve this particular problem?????
we can solve this problem by using wildcard in generics, which is denoted by ?. to solve above problem we need to use the wildcard like below.
[code lang=”java”] List<? extends Animal> list = new ArrayList<dog>();
? extends Animal means we can assign any List to the reference list provided it is of Animal type or subclass of animal type.
List<? extends Animal> list = new ArrayList<dog>();
List<? extends Animal> list = new ArrayList<cat>();
List<? extends Animal> list = new ArrayList<animal>();
[/code]

also read:

  • New Features in Java 5.0
  • Generics in Java 5.0<
  • Annotations in Java 5.0

all above three lines will compile provided Cat IS-A N Animal and DOG IS-AN Animal.but we can not add anything to the list, the reference has to be used to call methods so that we can get polymorphic behavior.

[code lang=”java”] public class Generics {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
List<dog> list1 = new ArrayList<dog>();
list1.add(new Dog());
iterateAnimals(list1);
}
private static void iterateAnimals(List<? extends Animal> list){
//list.add(new Dog());
for(Animal animal : list ){
System.out.println(animal.toString());
}
list.add(null);
}
}
class Animal{
public String toString() {
return "Animal";
}
}
class Dog extends Animal{
public String toString() {
return "Dog";
}
}
[/code] above code produces Dog as output. as we can see in the iterateAnimals() method body, we are not allowed to add anything to the list except null. only value allowed to add is null if we use ? extends syntax.

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: « Using ? super in generics – Java 5.0
Next Post: Mixing Generics And Non-Generics Code »

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

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

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