Q:

How can I write a getter for a class field whose dynamic type is different from declared type?

I have encountered a following situation at work, and I am looking for a better solution. There is a BaseClass, and one of its fields is declared to be of type FieldBaseClass. The field gets instantiated in child classes and has a dynamic type FieldChildClass then. I want to write a getter for the field that returns an instance of child class (FieldChildClass) and not of the parent. How can I achieve it without having to do a type cast? 
I have tried putting getter into child class and casting return type to specific child class, but this seems ugly.
BaseClass.java
public class BaseClass {

   protected FieldBaseClass field;

   public BaseClass() {
      field = calculateFieldValue();
   }

   public static class FieldBaseClass {
       protected String bar = "bar";
   }
}

ChildClass.java
public class ChildClass extends BaseClass {

   public FieldChildClass calculateFieldValue() {
       return new FieldChildClass("foo");
   }

   public static class FieldChildClass extends FieldBaseClass {
      private String foo;

      public FieldChildClass(String foo) {
         this.foo = foo;
      }
   }

   public FieldChildClass getField() {
      return (FieldChildClass) field;
   }
} 

A:

You can use a generic type e.g. T to define the type of the field.
public class BaseClass<T extends FieldBaseClass> {
  protected T field;

  public T getField() {
    return field;
  } 
}

public class ChildClass extends BaseClass<FieldChildClass> {

}

This will allow you to do:
ChildClass c = new ChildClass();
FieldChildClass f = c.getField();

