Question: Is there a way to use a subclass of the current class as a type parameter for a generic class ?
===============================================================================================

I'm not very sure how to expose my problem so I'll go with an example :

Example
-------

My `Connection` object is responsible for sending and receiving a certain type of (subclass of) `Message` over the network (only reproducing the relevant parts here) :

```
public class Connection<M extends Message> {
    public void send(M mes) {
        ...
    }
}

```

My `Message` object is an abstract class superclassing all kinds of messages sent over the network.

```
public abstract class Message {
    private transient Connection<?> conn;

    public void send() {
        this.conn.send(this);
    }
}

```

Of course, I have several subclasses of `Message`.

Problem
-------

My problem comes from the `Connection<?> conn` property of `Message` : in it's current state eclipse tells me that

> 
> The method send(capture#7-of ?) in the type Connection< capture#7-of ?> is not applicable for the arguments (Message)
> 
> 
> 

What type parameter should I use for `Connection` so that it is compatible with any subclass of `Message` ?

What I tried
------------

I tried declaring `Message` as :

```
public abstract class Message<M extends Message<M>> {
    private transient Connection<M> conn;
    ...
}

```

And then I would have :

```
public class MessageA extends Message<MessageA> {
    ...
}

```

as a subclass.

But this is a big hassle (I have a lot of generic classes using subclasses of `Message` as type parameters) and it doesn't seem to be the proper way to handle it.

Answer: 
Use `Connection<Message>`:

```
public abstract class Message {
    private transient Connection<Message> conn;
    ...
}

```
In order to get an `M` to give to `conn.send()`, you need an abstract method that will give you an `M`, which subclasses override to provide.

```
public abstract class Message<M> {
    abstract M getMessage();
    private transient Connection<M> conn;

    public void send() {
        conn.send(getMessage());
    }
}

public class MessageA extends Message<MessageA> {
    MessageA getMessage() {
        return this;
    }
}

```