Question: I'm facing the problem described in [this question](https://stackoverflow.com/questions/4031857/way-to-make-java-parent-class-method-return-object-of-child-class) but would like to find a solution (if possible) without all the casts and @SuppressWarning annotations.

A better solution would be one that builds upon the referenced one by:

* removing @SuppressWarning
* removing casts

Solutions presented here will be graded with 2 points based on the criteria. Bounty goes to solution with most points or the "most elegant" one if there is more than one with 2 points.

Answer: 
No cast, no @SuppressWarning, few lines only:

```java
public abstract class SuperClass<T extends SuperClass<T>> {
    protected T that;
    public T chain() {
        return that;
    }
}

public class SubClass1 extends SuperClass<SubClass1> {
    public SubClass1() {
        that = this;
    }
}

public class SubClass2 extends SuperClass<SubClass2> {
    public SubClass2() {
        that = this;
    }
}

```
One solution is to override the method in the child class and change the return type to a more specific one, ie. the child type. This requires casting. Instead of using the typical `(Child)` cast, use the [`Class#cast(Object)`](http://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#cast%28java.lang.Object%29) method

```
public class Parent {
    public Parent example() {
        System.out.println(this.getClass().getCanonicalName());
        return this;
    }
}

public class Child extends Parent {
    public Child example() {
        return Child.class.cast(super.example());
    }

    public Child method() {
        return this;
    }
}

```

The cast is hidden within the standard method. From the source of `Class`.

```
public T cast(Object obj) {
    if (obj != null && !isInstance(obj))
        throw new ClassCastException(cannotCastMsg(obj));
    return (T) obj;
}

```