39 lines
788 B
Java
39 lines
788 B
Java
// polymorphism/CovariantReturn.java
|
|
// (c)2016 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
|
|
|
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
|
|
*/
|