简体   繁体   中英

runtime/compile time polymorphism

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();

b2.subtract(); // ?????????**previously it was b1.subtract and its wrong 

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

}

}

I assume, judging by the title, that the code was in fact meant to be b2.subtract() . Going with that:

While b2 is currently an instance of Child, and it is quite easy to see that in your code it will always be an instance of Child, Java is statically typed and so can't allow you to use the methods of the actual class, just the declared class.

Consider this example:

Base b2 = Math.random() > 0.5 ? new Base() : new Child();
b2.subtract();

Now it's impossible to tell at compile time what b2 will actually be an instance of, so you obviously can't possibly call any methods in Child (as b2 may just be a plain Base!). It wouldn't really make sense for there to be an exception to this for cases like your example, as it would cause a lot of confusion and these cases can be easily corrected to work properly as-is (by changing the declared type to Child not Base).

Nothing happens in the JVM because the code will not compile. The method b1.subtract() cannot be resolved by the compiler because Base has no such member.

The class does not compile! Assuming you wanted to check if the base class object instantiated with a child class works or not, the answer is yes.

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

    Child b3 = new Child();

    b1.add();

    ((Child) b2).subtract(); // ?????????**

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

This will work and the out put it gives is as below.

Base ADD
Child Subtract
Child ADD
Child Subtract

This is what is called Runtime Polymorphism.

The b1 variable is of type Base . This class does not have a subtract() method, therefore it is failing to compile.

'cause Base doesn't have subtract.

b2 and b3 have subtract 'cause they're defined as Child.

[Edit:] No, wait, b2 doesn't have subtract, even though it is defined with new Child() it's still declared as a Base.

The call to b1.subtract() can be broken down into the classes being used.

b1 -> Base

subtract() -> Child

Since b1 is a Base , and not a Child the call to b1.subtract() will never work because b1 does not have a method called subtract() .

编译失败,因为BASE类没有减法方法.B1的类型为base。

subtract()方法不是Base类接口的一部分,这就是它失败的原因。

Java is strictly statically typed language. Parent class cannot point to the child class methods. At compile time Java compiler uses a closure principle and sees whether Base class has subtract method in it, as in this case there is no subtract method it results in compile time error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM