View RSS Feed

Java Basic

How to do Overloading with Generics

Rate this Entry
by , 11-29-2011 at 04:12 PM (2371 Views)
Overloading is a technique often used in object orientated programming languages such as java. In the context of generics unfortunately, classic type of overloading will produce a compile time error because of erasure.

Java Code:
import java.util.List;

public class OverloadingExample<W, T> {
	void f(List<T> v) {
	}

	void f(List<W> v) {
	}
}

What you must do to overcome this problem is to provide distinct names for all methods where the erased arguments donít produce a unique argument list. Note that this will be picked up by the compiler.

Java Code:
import java.util.List;

public class OverloadingExample2<W, T> {
	void f1(List<T> v) {
	}

	void f2(List<W> v) {
	}
}

Submit "How to do Overloading with Generics" to Facebook Submit "How to do Overloading with Generics" to Digg Submit "How to do Overloading with Generics" to del.icio.us Submit "How to do Overloading with Generics" to StumbleUpon Submit "How to do Overloading with Generics" to Google

Comments