Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional parametrization in Java with Generics

Tags:

java

generics

Is is it possible to specify default type when parametrzing a class? Example:

// abstract class    
public abstract class AbsClass<T1 extends Par1Class, T2 extends Par2Class> {
    // code 
}

// parametrized imlementation class
public class RealClass extends AbsClass<ClassThatExtendsPar1, ClassThatExtendsPar2Class> {
   // code
}

// non-parametrized imlementation class
public class RealClass extends AbsClass {
   // code
}

in my implementation I have to specify NONE or ALL parameters. Is possible to make the second parameter non-mandatory, something like this:

// abstract class    
public abstract class AbsClass<T1 extends Par1Class, T2 extends Par2Class : default Par2Class > {
    // code 
}

// parametrized only mandatory imlementation class
public class RealClass extends AbsClass<ClassThatExtendsPar1> {
   // code
}
like image 917
davorp Avatar asked Sep 04 '09 06:09

davorp


People also ask

How do you parameterize a generic class?

In order to use a generic type we must provide one type argument per type parameter that was declared for the generic type. The type argument list is a comma separated list that is delimited by angle brackets and follows the type name. The result is a so-called parameterized type.

How do I restrict a generic type in Java?

Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.

How do you declare a generic bounded type parameter in Java?

To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound, which in this example is Number . Note that, in this context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces).

Why primitive data types are not allowed in Java generics?

Since java is a Statically typed language where each variable and expression type is already known at compile-time, thus you can not define a new operation for such primitive types.


2 Answers

Simple answer: no, java does not support default parametrizations like that.

like image 56
Eugen Avatar answered Sep 21 '22 04:09

Eugen


There is never really a good reason to do this. Typically you specify generic type parameters because some method arguments take or return a parameter of that type. If unspecified were valid, that would seem to suggest you intended not to perform any meaningful implementation of those methods.

Anyway, to solve your perceived problem, just specify "Object" for any type parameter that you don't care to specify. Or extend the abstract class with another abstract class which has only one type parameter (specifying Object as the second type parameter in your extends call).

like image 28
Tim Bender Avatar answered Sep 21 '22 04:09

Tim Bender