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
}
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.
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.
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).
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.
Simple answer: no, java does not support default parametrizations like that.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With