Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Test {
- public static void main(String[] args) {
- C obj = new D(); // the object is actually a D
- A a = obj.method(); // actually calls the method returning a B
- System.out.println(a.getClass()); // its a B
- }
- }
- class A {
- }
- class B extends A {
- }
- // B is sub-class
- // B is 'narrower'
- class C {
- A method() {
- return new A();
- }
- }
- class D extends C {
- //Overriding method() in father class A
- B method() {
- return new B();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment