waltermilner

Untitled

May 9th, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.51 KB | None | 0 0
  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
  27.  
  28.     B method() {
  29.         return new B();
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment