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: 
One approach is to define an abstract method `getThis()` in `Parent` class, and make all the `Child` classes override it, returning the `this` reference. This is a way to recover the type of `this` object in a class hierarchy.

The code would look like this:

```
abstract class Parent<T extends Parent<T>> {

    protected abstract T getThis();

    public T example() {
        System.out.println(this.getClass().getCanonicalName());
        return getThis();          
    }
}

class ChildA extends Parent<ChildA> {

    @Override
    protected ChildA getThis() {
        return this;
    }

    public ChildA childAMethod() {
        System.out.println(this.getClass().getCanonicalName());
        return this;
    }
}

class ChildB extends Parent<ChildB> {

    @Override
    protected ChildB getThis() {
        return this;
    }

    public ChildB childBMethod() {
        return this;
    }
}

public class Main {

    public static void main(String[] args) throws NoSuchMethodException {
        ChildA childA = new ChildA();
        ChildB childB = new ChildB();

        childA.example().childAMethod().example();
        childB.example().childBMethod().example();
    }
}

```

As per requirement, there is no *Casting* and no *@SuppressWarnings*. I learnt this trick few days back from [Angelika Langer - Java Generics FAQs](http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html).

**Reference:**

* [How do I recover the actual type of the this object in a class hierarchy?](http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#How%20do%20I%20recover%20the%20actual%20type%20of%20the%20this%20object%20in%20a%20class%20hierarchy?)
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;
}

```