Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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

Is any difference? "wild card generics"

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

Is there an difference between these two generic class definitions:


public class MyList<T extends Comparable> {
}

and

public class MyList<T extends Comparable<? super T>> {
}

Thanks for reply in advance!
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is. The explanation is quite odd, quite difficult, and if you use those 2 class declarations or something similar in a job you would put your co-workers in trouble.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Still nobody gave me detailed explanation! I urge Bert to give me explanation,
Thanks,
cmbhatt
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

1)Actually <T extends Comparable> is used to filter the usasge of non-Comparable Object types.

2)it seems there is a bug here ... but gone undected.

public class MyList<T extends Comparable<? super T>> {
}

Now if you think to have a comparable like
class MyClass implements Comparable<? super T> is non-sense to have.

rather we can have something like this to pass
class MyClass implements Comparable<T> (or)
class MyClass implements Comparable<T extends SomeSuperClass> (or)
class MyClass implements Comparable<T super SomeSubClass>.

<? super T> can be used in functions parameter like...
public void addMe(List<? super T> lst). This has meaning like ... List lst can add only T type.

Hope you got my point.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srini!

Actually a more restrictive form of generic class may be

class MyList<T extends Comparable<T>> {
}

and if parent class implements Comparable, as usually the child implements but with this it is not the case, your child class also needs to implement the Comparable interface to write like :

class Base implememts Comparable<Base> {

}
MyList<Base> b1 = new MyList<Base>();
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srini!

Actually a more restrictive form of generic class may be

class MyList<T extends Comparable<T>> {
}

and if parent class implements Comparable, as usually the child implements but with this it is not the case, your child class also needs to implement the Comparable interface to write like :

class Person implememts Comparable<Person> {
public int compareTo(Person p1) {
return 0;//assume as it does so
}
}

class Manager extends Person{
}
MyList<Person> b1 = new MyList<Person>();
But you can't write like this:
MyList<Manager> m1 = new MyList<Manager>();

because your generic class definition requires that every class should implement the Comparable interface.

That is why we write
class MyList<T extends Comparable<? super T>{
}

Now the following is OK!
MyList<Manager> m1 = new MyList<Manager>();

I would like to have more info if you have!
But what my previous post says, i am eager to know whether both have same meanings.

Thanks Srini for attention!

[ March 04, 2007: Message edited by: Chandra Bhatt ]
[ March 04, 2007: Message edited by: Chandra Bhatt ]
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

What do you really understand by

class Person<T extends Comparable<? super T>{
}

How you are interpreting the above class Person ???
Here Person does not implement Comparable. How can you do the fallowing

MyList<Person> m = new MyList<Person> with the above Person declaraion

[ March 01, 2007: Message edited by: Srinivasan thoyyeti ]
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:

MyList<Manager> m1 = new Manager<Manager>();



How you are assigning a Manager to MyList class reference.
what is the relationship between them.


Interpret correctly.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you can see it has to be He obviously simply made a typo...
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

First i have to say, your explanation confused me a lot. becuase i don't know he concept before. Last night i saw sun book. then i got "what you want to say !"

I thank you for showing the notation: class MyClas<T extends Comparable<? super T>>. Though your explanation confusing you did a great job, Keep it up.

Here is the clear cut code: which is self explanatory

class MyList<T extends Comparable<? super T>>{
}

class Employee implements Comparable<Employee>{
public int compareTo(Employee o){
return 0; // just added to suffice int return
}
}

class Manager extends Employee{
}

public class SuperTest{
public static void main(String... a) {

MyList<Employee> empList = new MyList<Employee>();

MyList<Manager> mgrList = new MyList<Manager>();
/* MyList<Manager> is legal here because of notation "<? super T>"

Checks like ...
1)if T declared implements Comparable<T> or the super class did it or not.

2)if substitute Manager for T then ---> <? super Manager>.

3)Means does Manager or the super class implemented it ? here in our case super class Employee did it. Hence it is valid.
*/
}
}
Thanks again Chandra,

[ March 01, 2007: Message edited by: Srinivasan thoyyeti ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srini!

Thanks for pretty consideration,

I am sorry, it was just a typo, but i can understand it can mislead anyone,

class Person implements Comparable<Person> {
}

class Manager extends Person {
}


class MyList<T extends Comparable<T>>{}
This definition demands each class should implement Comparable interface and if mere your parent or super implements the Comparable, it wont help.
Simply you can't write
MyList<Manager> list1 = new MyList<Manager>();

Because your generic class definition demands the Manager must also implement the Comparable but this restriction is undesirable. I assure you whereever in Standard generic class you will See like

class MyList<T extends Comparable<? super T>{}
It is the most desirable approach. Because it says if the super class of the class implements the Comparable that is also OK. that happens in our Person and Manager case. Person implements the Comparable interface and Manager extends the Person. It will do!

Dear Srini, I hope this information helps you to alleviate with the last night confusion. Is that OK? Please initimate me regarding this

Regards,
cmbhatt
[ March 04, 2007: Message edited by: Chandra Bhatt ]
 
Whatever. Here's a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic