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

3
  • im a c++ programmer too, slightly dabbling in java, and to me that looks like STL template definitions :) Commented Apr 26, 2011 at 3:06
  • duplicate of many like: stackoverflow.com/questions/490091/java-generics Commented Apr 26, 2011 at 3:06
  • +1 for @MeBigFatGuy's username-lol Commented Apr 26, 2011 at 3:07

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, can u provide some links explaining java classes taking parameters
@Kazoom: Those classes are called "generic" classes. Read the link given by MeBigFatGuy in the question's comments.
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

1 Comment

It might not have pure virtuals. Abstract classes in Java only have the property of not being directly instantiatable in and of themselves.
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.

Comments

-1

The angle bracket notation is for Java Generics.

5 Comments

-1 You'd think that C++ people already know what generics are, surely? C++ templates are much beefier than Java generics.
the main difference being in c++ each specialization creates a separate class, whereas in java the specialization is lost at compile time.
@MeBigFatGuy: well... after compilation phase is complete... Java generics just aren't reified like C++ generics are.
@Chris: Generics != C++ templates, and it's not clear the OP had run across the term before.
+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!
-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.