Questions tagged [generic-method]

180 questions
51
votes
6 answers

Java generic method inheritance and override rules

I have an abstract class that has a generic method and I want to override the generic method by substituting specific types for the generic parameter. So in pseudo-code I have the following: public abstract class GetAndParse { public SomeClass…
David K.
  • 6,153
  • 10
  • 47
  • 78
37
votes
5 answers

Assigning List of Integer Into a List of String

I was learning Generics in Java and I came close to a very interesting piece of code. I know in Java it is illegal to add list of one type to another. List integerList = new ArrayList(); List stringList=integerList; So in…
mirmdasif
  • 6,014
  • 2
  • 22
  • 28
36
votes
1 answer

Why can this generic method with a bound return any type?

Why does the following code compile? The method IElement.getX(String) returns an instance of the type IElement or of subclasses thereof. The code in the Main class invokes the getX(String) method. The compiler allows to store the return value to a…
nrainer
  • 2,542
  • 2
  • 23
  • 35
32
votes
5 answers

How can C# allow virtual generic methods where C++ can't allow virtual template methods?

C++ does not support virtual template methods. The reason is that this would alter the vtable whenever a new instantiation of such a method is made (it has to be added to the vtable). Java in contrast does allow virtual generic methods. Here, it is…
gexicide
  • 38,535
  • 21
  • 92
  • 152
31
votes
6 answers

Java generic methods in generics classes

If you create a generic class in Java (the class has generic type parameters), can you use generic methods (the method takes generic type parameters)? Consider the following example: public class MyClass { public K doSomething(K k){ return…
amaidment
  • 6,942
  • 5
  • 52
  • 88
29
votes
5 answers

Combining Raw Types and Generic Methods

Here's a question, this first code listing compiles just fine (JDK 1.6 | JDK 1.7): ArrayList a = new ArrayList(); String[] s = a.toArray(new String[0]); However, if I declare the List reference as a raw type: ArrayList a = new…
Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
28
votes
3 answers

Invoking Java Generic Methods

I am studying Java generic feature and I am not sure how to explain the third line in the following main method: public class Example4 { public static void main(final String[] args) { System.out.println(Util.compare("a", "b")); …
25
votes
2 answers

How to create a generic method in Dart?

I'm trying to use generic methods in Dart (1.22.0-dev.10.3). Here is a simple example: abstract class VR { VR(); bool foo(T value); } class VRInt extends VR { VRInt(); bool foo(int n) => n > 0; // Thinks n is…
jfp
  • 381
  • 1
  • 3
  • 8
21
votes
5 answers

scala generic method overriding

I have an abstract class : abstract class Foo(...){ def bar1(f : Foo) : Boolean def bar2(f : Foo) : Foo } multiple classes extend Foo and override the methods class FooImpl(...) extends Foo{ override def bar1(f : Foo) : Boolean { …
Jannik Luyten
  • 213
  • 1
  • 2
  • 5
20
votes
4 answers

Difference between generic super class and super class type

I can't understand the difference between the two code snippets below. Can someone help me with a simple explanation? First of all, I have to say that I have a lot of classes that extend a super class named BaseEntity, so what are the differences,…
user4018604
17
votes
2 answers

C# Generic Method Without Specifying Type

Ok so I'm a Java guy starting to use C# and I was coding and started making a generic method and what I wrote runs and compiles but it goes against everything I know about how generics should work so I'm hoping someone can explain this to me: So I…
Ian Dallas
  • 12,451
  • 19
  • 58
  • 82
14
votes
3 answers

Generic methods and optional arguments

Is it possible to write similar construction? I want to set, somehow, default value for argument of type T. private T GetNumericVal(string sColName, T defVal = 0) { string sVal = GetStrVal(sColName); T nRes; if…
hardsky
  • 514
  • 5
  • 15
13
votes
2 answers

Why is a parameter's private field visible to a generic method in Java 6 but not in Java 7?

Possible Duplicate: Type-parameterized field of a generic class becomes invisible after upgrading to Java 7 public class Test{ private String _canYouSeeMe = "yes"; void genericMethod(T hey){ String s =…
meta-meta
  • 926
  • 5
  • 11
10
votes
5 answers

Operator '&' cannot be applied to operands of type 'T' and 'T'

My application defines several enums that include the [Flags] attribute. I wanted to write a small utility method to check if a flag was set for any of those enums and I came up with the following. protected static bool IsFlagSet(ref T value, ref…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
10
votes
3 answers

Generic method handles IEnumerable differently than generic type

Please check the following codes segments: public interface ICountable { } public class Counter where T : ICountable { public int Count(IEnumerable items) { return 0; } public int Count(T Item) { return…
Daniel Leiszen
  • 1,827
  • 20
  • 39
1
2 3
11 12