To understand this topic, you need to know what compilation processes and runtimes are in general. In short, when you create your application compiler, it goes through all your code and checks for consistency, security, and the ability to run. If there is no error created by the compiler, it generates class files from your source code ( java files). When the application is running, it means that your class files are loaded into memory, and the JVM follows the instructions for your application.
In your example:
Person a = new Student();
Compilation process: The compiler checks this line: Person a = new Student(); for type safety (compatibility). So, if a student is a compilation of a person proceeds to the next line, he fails. In the next line: a.speak(); the compiler looks at type a , finds that it is Person and looks for the speak() method in Person . If this method is not based on the compiler, the compilation process fails.
Execution Process: When the JVM executes this line: Person a = new Student(); it goes through the initialization process from the top (parent class) to the bottom (child class). In the next line: a.speak(); The JVM detects the student object via a link, looks for the speak() method, if it is based on student , then executes it, otherwise it runs the speak() method from the parent class Person .
Other examples from the subject of inheritance:
class Person { public void speak() {} public void think() {} } class Student extends Person { @Override public void speak() {} public void speakALot() {} } Person a = new Student(); a.speak();
source share