Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Illustrate covariant return type
- public class Test {
- public static void main(String[] args) {
- B b = new B();
- System.out.println(b.m()); // I'm a Sub instance
- }
- }
- class Base { // a base class
- }
- class Sub extends Base { // a sub class
- public String toString() {
- return "I'm a Sub instance";
- }
- }
- class A {
- Base m() { // covariant return type
- return new Base();
- }
- }
- class B extends A {
- Sub m() { // sub is 'narrower' than base
- return new Sub();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment