SHOW:
|
|
- or go back to the newest paste.
| 1 | public class Test {
| |
| 2 | ||
| 3 | public static void main(String[] args) {
| |
| 4 | C obj = new D(); // the object is actually a D | |
| 5 | A a = obj.method(); // actually calls the method returning a B | |
| 6 | System.out.println(a.getClass()); // its a B | |
| 7 | } | |
| 8 | } | |
| 9 | ||
| 10 | class A {
| |
| 11 | } | |
| 12 | ||
| 13 | class B extends A {
| |
| 14 | } | |
| 15 | // B is sub-class | |
| 16 | // B is 'narrower' | |
| 17 | ||
| 18 | class C {
| |
| 19 | ||
| 20 | A method() {
| |
| 21 | return new A(); | |
| 22 | } | |
| 23 | } | |
| 24 | ||
| 25 | class D extends C {
| |
| 26 | - | //Overriding getFoo() in father class A |
| 26 | + | //Overriding method() in father class A |
| 27 | ||
| 28 | B method() {
| |
| 29 | return new B(); | |
| 30 | } | |
| 31 | } |