Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Paul Clapham
Ron McLeod
Jeanne Boyarsky
Tim Cooke
Sheriffs:
Liutauras Vilda
paul wheaton
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Carey Brown
Frits Walraven
Bartenders:
Piet Souris
Himai Minh
Forum:
Programmer Certification (OCPJP)
wildcard type parameter
Bob CHOI
Ranch Hand
Posts: 127
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
i wrote following code snippet for understanding "wildcard generic type", or i'd prefer to call it "wildcard type parameter".
pls comment whether i've got the essence behind the scenes. Thanks!
package mypkg; import java.util.*; class MyGenericTest { public static void main(String[] args) { MyList<Animal> animalHouse = new MyList<Animal>(); //create the object of "BaseType<SuperType>" MyList<Cat> catHouse = new MyList<Cat>(); //create the object of "BaseType<SubType>" for (int i=0;i<10;i++) { catHouse.seet(new Cat()); animalHouse.aadd(catHouse); //pass the object of "BaseType<SubType>" to "BaseType<SuperType>" } MyList<Dog> dogHouse = new MyList<Dog>(); //create the object of "BaseType<OtherSubType>" for (int i=0;i<10;i++) { dogHouse.seet(new Dog()); animalHouse.aadd(dogHouse); //pass the object of "BaseType<OtherSubType>" to "BaseType<SuperType>" } for(Animal animal:animalHouse.geet()) //retrieve the objects of mixed sub types System.out.println(animal); } } class MyList<E extends Animal> { private List<E> myList = new ArrayList<E>(); private E e = null; void seet(E e) { this.e = e; } void aadd(MyList<? extends E> myList) { //"wildcard generic type" or "wildcard type parameter" declaration to allow type parameter subcasting this.myList.add(myList.e); } List<E> geet() { return myList; } } class Animal {} class Cat extends Animal {} class Dog extends Animal {}
(added
tags)
[ December 01, 2006: Message edited by: Barry Gaunt ]
Hard work rewards
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
generics
Need Explanation for a question in Generics
Generics Doubt
Generics
ClassCastException
More...