OnJava8-Examples/polymorphism/CovariantReturn.java
Bruce Eckel 49edcc8b17 reorg
2015-06-15 17:47:35 -07:00

36 lines
654 B
Java

//: polymorphism/CovariantReturn.java
// ©2015 MindView LLC: see Copyright.txt
class Grain {
@Override
public String toString() { return "Grain"; }
}
class Wheat extends Grain {
@Override
public String toString() { return "Wheat"; }
}
class Mill {
Grain process() { return new Grain(); }
}
class WheatMill extends Mill {
@Override
Wheat process() { return new Wheat(); }
}
public class CovariantReturn {
public static void main(String[] args) {
Mill m = new Mill();
Grain g = m.process();
System.out.println(g);
m = new WheatMill();
g = m.process();
System.out.println(g);
}
} /* Output:
Grain
Wheat
*///:~