Home > Java > javaTutorial > body text

Analysis of essential examples of method rewriting in java

WBOY
Release: 2023-05-04 10:04:14
forward
1451 people have browsed it

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...");
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!