1

I'm studying code. I 've found these two classes. I don't understand how they are related. What does the Expression "T extends PieceType" mean, what does the T stand for?

piece.java:

public interface Piece<T extends PieceType> {

    /**
     * Returns the color.
     * @return the color
     */
    PieceColor getColor();    

    /**
     * Returns the type.
     * @return the type
     */
    T getType();
}

pieceType.java:

public interface PieceType {

    /**
     * Returns the type's base rating.
     * @return the base rating within range [0, 1000]
     */
    double getRating();
}
2
  • 4
    You need to read about Generics. Start here. Commented Jul 25, 2013 at 13:30
  • After you digest that, move on to here. Commented Jul 25, 2013 at 13:34

2 Answers 2

4

As mentioned, it means that the type, T, that you pass into Piece must extend PieceType.

Here we have an interface that extends PieceType:

public interface NewPiece extends PieceType {
    ...
}

You would then instantiate a Piece object by doing this:

Piece<NewPiece> aPiece = new SomeImplementationOfPiece<NewPiece>();

Because NewPiece extends PieceType as given in your definition:

public interface Piece<T extends PieceType> { ... }
Sign up to request clarification or add additional context in comments.

Comments

3

The interface Piece is generic, and its single type parameter T must implement PieceType, as indicated by T extends PieceType. It might behoove you to read further into generics; the linked lesson should be a good start.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.