• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this the correct understanding of generics?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class NonEmptyTree<K extends Comparable<K>, V> implements Tree<K, V> {

I understand if we have something like:
public class MyClass {
}

but what does it mean to have a class name and right along the class name these <> and stuff inside those angle brackets?
and what does it mean to "extends" the Comparable interface
Is K and V a class type, what's going on there?

Really this whole line "K extends Comparable<K>, V" I don't understand and why it's beside the class name. Can someone break down what's happening in there?

I think I get the implements part, it's saying it implements the tree interface therefore we have to fill out those prototype methods in our class, but still lost on the K and V stuff.


Please help
Thank you
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's Java's Generics feature. And it is probably better explained with a tutorial...

https://docs.oracle.com/javase/tutorial/java/generics/

As it is not something minor...

Henry

Edit Note: When the original topic was merged with a follow up topic, it used the subject title from the follow up topic. The original topic title had no mention of "generics", and hence, this response was a bit more valuable.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A tree requires its elements be Comparable to one another. So you try saying Tree<Comparable> and add a String and an Integer which both implement Comparable and all sorts of things will go wrong. So you need to check that the objects are Comparable to something. String implements Comparable<String> and Integer implements Comparable<Integer>, so you think, a type must be Comparable to itself. So you have to put the K to the left and to the right:-
TreeSet<K extends Comparable<K>>
So far, so good. Remember that in generics you always say extends rather than implements. But what if you have a superclass which implements Comparable<Itself>? What if you are putting several different subtypes of it into the same TreeSet? Nothing wrong with that, but what are they going to be Comparable to? It has to be the superclass? And what is the superclass? Don't know. So you have to say they are Comparable to some unknown supertype of the current type. That is written ? super K. So we end up with
TreeSet<K extends Comparable<? super K>>
Done. In a Map you have two kinds of object, so you have Key and Value and you always say a Map is of type <K, V>.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I read:



Is this the same as saying that in this data structure the only T type things which can be accepted are those which first implemented the Comparable interface with the <T> specified in their class declaration?

Example:



and



Only objects derived from class declaration as these
would be accepted in the MyList data structure?

Or is it saying that the restriction is only for Objects from classes that implement Comparable of THEIR specific type such as:



and




Thanks!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the latter.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:It's the latter.


Thank you
Could you please give more detail to why. I've been trying to understand the whole concept here.

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, one person told me it's the first now I hear it's the second. Please I need to fully understand this concept.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding is that its the second.
Basically in any one declaration you can substitute all instances of 'T' with one Type.

so given:
public class MyList<T extends Comparable<T>> {}

Both T's have to be substituted with the same Type
All generic type placeholders have to be substituted before you can "use" the type.

The 'T' in one type expression does not have any correlation with another one.

Consider the type List<T>
You can have a list of
List<String>   - T is String
List<Number> - T is Number
List<List<String>> - T is List<U>, and U is String
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi justin,
first of try to understand what this signature means:

here the type parameter for your MyList is "T".now their is bound on T and it is-T should be a subtype of Comparable<T>.means the declaration for T should be like:
this is a recursive bound it means T type instances can be compared to itself i.e.,they are mutually comparable.so your data structure needs to take a type which can be compared to itself.

now your former case is:

here the Dog is implementing the comparable<T>,means Dog type instances can be compared to T type instance and since you have not put a bound on T,compiler will infer T as an object.so you are declaring Dog type instances are comparable to Object type,which is not your requirement your data structure needs to be parametrized on type which are comparable to itself but not on which are comparable to object.same is for the cats..

Kind Regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Though the former code will not compile,my previous explanation is for your understanding purpose.another approach may be:
here compiler does not know what actually is T,it can be a object,String,Integer...etc..due to all these confusion compiler will complain for-it cannot find symbol T.
 
timothy adigun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:Though the former code will not compile,my previous explanation is for your understanding purpose.another approach may be:
here compiler does not know what actually is T,it can be a object,String,Integer...etc..due to all these confusion compiler will complain for-it cannot find symbol T.


And to add to that. T is just a placeholder.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Robbins wrote:. . . . . .
Surely it's public class Dog, not Dogs.
On Tuesday, I wrote:String implements Comparable<String> and Integer implements Comparable<Integer>, so you think, a type must be Comparable to itself.
That was in your other thread. Remember you are comparing a Dog with another object of the same class, and I won't even start about cats

I think the two threads are so similar that I shall merge the two discussions.
 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Understood.
Probably doesn't make much sense for when the Dog class is created to compare it <T> things? does it? when we implement its Comparable interface, we want things that can compare with what they are so when they are passed into the <T> parameter we can easily compare them against their own type not someone else's. For example all Strings, or all Dogs or all Integers we want one type and one type only. So what they originally must have been is comparing themselves.

Example:


The only things that go into that T type are only which implement the comparable interface and that when they implemented the comparable interface they did so with comparing things of their same type.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or their supertypes; remember that if the supertype implements Comparable<Animal> then Dog will also implement Comparable<Animal>.

How do you compare a Dog to another Dog?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Or their supertypes; remember that if the supertype implements Comparable<Animal> then Dog will also implement Comparable<Animal>.

How do you compare a Dog to another Dog?
well said,
does this declaration works in that situation:

this declaration will take care of that T type can be compared to its supertypes and itself(as each type is a supertype of itself).is it correct,campbell.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:. . . is it correct,campbell.
There is a very very good explanation of ? super T above in this very thread. Well, maybe not very very good
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay got it,one last question-can you guide me how should i read this declaration-Enum<E extends Enum<E>>,it really irritates me when i tried to understand it analytically.can you please do me a favor for the same declaration to understand.

Kind Regards,
Praveen.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know that the Enum class is the superclass of all enums. So each enum is a subclass of Enum. So if the enum type is Month, then you can regard its superclass as being Enum<Month>. But you don't want Enum<String> because String isn't an enum type. So you have to specify that the type is a subclass of Enum. But you don't want Enum<Month> being a subtype of Enum<Day> or anything like that. So the superclass of a type of enum has to be an Enum of the same type. So the Month enum has to be a subtype of Enum<Month> and <Month> has to be a subtype of Enum<Month>.

I am sure there are lots of other people who can explain that much better.
 
Knute Snortum
Sheriff
Posts: 4287
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's pretty good, though.  It helped me understand it!
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry campbell,but everything you tried goes over my head.
i think i get some points:
-->as you mentioned if Month is a enum,this declaration assure that only Enum<Month> can be its supertype despite of others like Enum<someclass>,situation will get worst if that someclass is extending someOtherClass--it will break java single inheritance paradigm.
-->E extends Enum<E> provides a type safety for the sub type E of Enum<E> to several inherited methods.
Through inheritance several methods of enum is inherited by the subclass E objects.like take an example of compareTo() method the parameterized type<E extends Enum<E>> lets this method to take E as a argument though in superclass it takes the Enum type as a argument so providing a type safety to the method argument.

is these points,stated  above,are correct and explains for the such generic type declaration???

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!