4

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

MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
Kazoom
  • 5,659
  • 16
  • 56
  • 69

5 Answers5

3

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.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
1

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

Sasha O
  • 3,710
  • 2
  • 35
  • 45
  • 1
    It might not have pure virtuals. Abstract classes in Java only have the property of not being directly instantiatable in and of themselves. – Joseph Ottinger Apr 26 '11 at 03:10
1

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.

Bala R
  • 107,317
  • 23
  • 199
  • 210
-1

The angle bracket notation is for Java Generics.

Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
  • -1 You'd think that C++ people already know what generics are, surely? C++ templates are much beefier than Java generics. – C. K. Young Apr 26 '11 at 03:09
  • 1
    the main difference being in c++ each specialization creates a separate class, whereas in java the specialization is lost at compile time. – MeBigFatGuy Apr 26 '11 at 03:11
  • @MeBigFatGuy: well... *after* compilation phase is complete... Java generics just aren't reified like C++ generics are. – Joseph Ottinger Apr 26 '11 at 03:12
  • 2
    @Chris: Generics != C++ templates, and it's not clear the OP had run across the term before. – Jim Ferrans Apr 26 '11 at 03:14
  • 1
    +1 to @Jim: Java Generics and C++ templates share only superficial similarity. They use a similar syntax and Java generics approach a subset of the problems approached by C++ templates, but their implementation and are quite different! – Joachim Sauer Apr 26 '11 at 07:13
-1

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.

Joseph Ottinger
  • 4,911
  • 1
  • 22
  • 23