I am trying to understand what is this java class definition.
abstract public class A<P extends B<?, ?>,Input,Output>
{
...
// class defined
...
}
A c++ programmer moving to java
I am trying to understand what is this java class definition.
abstract public class A<P extends B<?, ?>,Input,Output>
{
...
// class defined
...
}
A c++ programmer moving to java
This defines an abstract class called A
, with three type parameters:
P
, which must be of type B
(with any type arguments) or any type derived from itInput
, of any typeOutput
, of any typeOf interest is the first type parameter. In C++, for a type-based template parameter, you can supply any type; in Java, you have the option to constrain the type by what class and/or interfaces such a type must also extend/implement.
A bit of "translation":
"abstract" means this class may have abstract (~=pure virtual) methods.
class A is a generic (~template) definition
P extends ... is an extra constraint on generic parameter, should be subclass of ...
P extends B<?, ?> means that the generic parameter#1 is a subclass of another generic class
It's an abstract class definition (obviously) with 3 generic parameters.
The first parameter P has a constraint that it has to be of type (or that extends) class/interface B which has two generic parameters (no constraint on those) so it could be like
public class B<T1, T2> {
}
The second and third parameters namely Input and Output have no constraints.
The angle bracket notation is for Java Generics.
Well, to really understand it you'd want to include more of the definition, but B is a class with generics itself, and A has three generic references in it; it's a bit pathological, but it's fairly easy to step through.