Home  >  Article  >  Java  >  Analysis of essential examples of method rewriting in java

Analysis of essential examples of method rewriting in java

WBOY
WBOYforward
2023-05-04 10:04:141454browse

Essential description

1. During the compilation phase, the compiler only knows the static type of the object, but not the actual type. Therefore, it can only determine the method of calling the parent class in the class file. method.

2. During execution, it will determine the actual type of the object. If the actual type implements this method, it will be called directly. If not implemented, it will be retrieved from bottom to top based on inheritance. As soon as it is retrieved, it will be called. If not retrieved, it is discarded.

Example

class Animal {
    void eat() {
        System.out.println("eating...");
    }
}
 
class Dog extends Animal {
    void eat() {
        System.out.println("eating bread...");
    }
}

The above is the detailed content of Analysis of essential examples of method rewriting in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete