Kazoom
Kazoom

Reputation: 5849

Java class definition

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

Upvotes: 4

Views: 1549

Answers (5)

Bala R
Bala R

Reputation: 108937

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.

Upvotes: 1

Sasha O
Sasha O

Reputation: 3749

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

Upvotes: 1

C. K. Young
C. K. Young

Reputation: 222973

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 it
  • Input, of any type
  • Output, of any type

Of 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.

Upvotes: 3

Joseph Ottinger
Joseph Ottinger

Reputation: 4951

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.

Upvotes: -1

Jim Ferrans
Jim Ferrans

Reputation: 31012

The angle bracket notation is for Java Generics.

Upvotes: -1

Related Questions