DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Kieron has posted 1 posts at DZone. View Full User Profile

Covariant Return Types In Java 5

  • submit to reddit
        Surprisingly legal code under Java 5, using covariant return types.

public class CovarientReturn {

  static class A {
    public Number getNumber() {
      return new Integer(1);

  static class B extends A {
    public Integer getNumber() {
      return new Integer(1);

  public static void main(String[] args) {
    A a = new A();
    B b = new B();

    Number n = a.getNumber();
    Integer i = b.getNumber();