Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Covariant Return Types In Java 5

DZone's Guide to

Covariant Return Types In Java 5

·
Free Resource
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();
  }

}
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}