runtime/compile time polymorphism

Posted by dmadhavaraj on Stack Overflow See other posts from Stack Overflow or by dmadhavaraj
Published on 2010-03-19T06:03:03Z Indexed on 2010/03/19 6:11 UTC
Read the original article Hit count: 208

Filed under:

Hi ,

In the below code , why b1.subtract() fails . Please explain me the reason ie., what happens in JVM while invoking that method .

class Base {

public void add() {
System.out.println("Base ADD");
}

}

class Child extends Base {

public void add(){
System.out.println("Child ADD");
}

public void subtract() {
System.out.println("Child Subtract");
}

}

class MainClass {

public static void main(String args[]) {

Base b1 = new Base();
Base b2 = new Child();

Child b3 = new Child();

b1.add();

b1.subtract(); // ?????????**

b2.add();
b3.subtract();

}

}

© Stack Overflow or respective owner

Related posts about java