Can a java method implementing a base class method return an inherited class?

Viewed 1883

Let's say I have the standard Draw class with inherited classes of Circle and Square. Is there a way to do the following? Maybe with generics?

class Draw {
  public abstract Draw duplicate();
}

class Circle extends Draw {
  public Circle duplicate() {
    return new Circle();
  }
}

class Square extends Draw {
  public Square duplicate() {
    return new Square();
  }
}
4 Answers
Related