Shay Levy

Greenhorn
+ Follow
since Oct 01, 2009
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Shay Levy

Hey Stanley,

The explanation I provided was wrong. read Henry's explanation, It is the correct one.
14 years ago
Thanks Henry. being new to generics is really confusing at the beginning. what i understood from your answer is that T and Object are equivalant in the context of class declarations : Sport<T> is the same as Sport<Object> only the latter can create confusion when used. So the polymorphism issue is only at methods declarations?

Thanks
14 years ago
Hi Stanley,

I am pretty new to Generics but from what I understand the two implementations you described of Sport class are not the same. Parameterized types are not polymorphic which means that the Sport<Object> class can only be instantiate with a type Object and not with any other type (even though they are Object too). On the other hand the Sport<T> class can be instantiated with any type (String,Integer,Double or types that you created).

As for the second question. A popular example is the generic Stack implementation. If you implement a Stack using generics (class MyStack<E>) than your stack could hold many types. you can instantiate one stack that will hold String elements and another that will hold Integer elements and so on. The most important thing is that you dont have to worry about type-safety issues (assuming you dont try to interact with legacy code, but that is for a diffrent topic).

I am sure some of the Bartenders and Ranch handers can give a more detailed explanation.
14 years ago
Hi John,



The above code generate compiler error:
incompatible types
found: stronger
required: [PackageName.Animal]

Animal and Dog implements Comparable<Animal>, So I dont understand why i get a compiler error

Thanks
14 years ago
Hello,

I was looking at a snippet of code that was part of an example for flexible comparisons with wildcard and there was something that I didnt understand.
First here is the the code (I changed it a little bit because the names were longer in the book)



The following method suppose to determain who is stronger:



I understand that line (1) cannot compile because dog is not Comparable<Dog> but Comparable<Animal>.
I also understand that line (2) can work because the type parameter that is passed (Animal) satisfies the method signiture.
what I dont understand is why line (3) is working, what is the compiler checking that enables line (3) but not (1)? What is the Type (T) that is actually used when executing line (3)?

I hpoe my questions were clear.

Thanks

14 years ago
Hi,


(1) interface A {}
(2) class B implements A{}

(3) A a = new B();
(4) B b = new B();

is there any diffrence between statement (3) and (4)? Is the declaration of (3) better (due to polomorphism) ? do I gain anything by using (3) instead of (4)? or did I just miss something really basic?

Thanks
14 years ago