001package edu.pdx.cs410J.j2se15;
002
003/**
004 * This class demonstrates <I>covariant returns</I> in JDK 1.5.  The
005 * Java compiler allows an overriding method to change the method's
006 * return type to be a subclass of the overriden method's return
007 * type.  However, you have to be extra careful when using covariant
008 * returns with legacy code.  If someone else overrides a method and
009 * doesn't change the return type, their code will not compile.
010 *
011 * @author David Whitlock
012 * @since Winter 2004
013 */
014public class CovariantReturns {
015
016  /**
017   * An animal that can be cloned.  Such pointed political commentary.
018   */
019  static abstract class Animal implements Cloneable {
020
021    public abstract Object clone();
022
023  }
024
025  static class Human extends Animal {
026
027    public Human clone() {
028      return new Human();
029    }
030  }
031
032  static class Student extends Human {
033
034    public Student clone() {
035      return new Student();
036    }
037  }
038
039  /**
040   * A main class that clones some animals.  Note that we don't need
041   * to cast the result of the <code>clone</code> method.
042   */
043  public static void main(String[] args) {
044    Human human = new Human();
045    @SuppressWarnings("unused")
046    Human human2 = human.clone();
047
048    Student student = new Student();
049    @SuppressWarnings("unused")
050    Student student2 = student.clone();
051  }
052
053}