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

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

public class CovarientReturn {

  static class A {
    public Number getNumber() {
      System.out.println("Number");
      return new Integer(1);
    }
  }

  static class B extends A {
    public Integer getNumber() {
      System.out.println("Integer");
      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();
  }

}