Type checking)It is the process of confirming the type of any expression and ensuring that various statements comply with the type restriction rules. Java is a language for static type check, but it still requires runtime type check and throws possible runtime exceptions.
Wiki:
Static type-checking is the process of verifying the type safety of a program based on analysis of a program's source code.
Dynamic type-checking is the process of verifying the type safety of a program at runtime
package typeSystem;import static tips.Print.*;//pln()class Sup{ Sup me() { return this;} public void inSup() {pln("Sup");}}class Sub extends Sup { public void inSub() {pln("Sub");}}
① What is returned by "new sub (). Me?
A: The actual type of the returned object sub. The declared type Sup. The compiler only knows that me () returns sup in sup. The runtime subclass inherits me () and returns this.
② New sub (). Me (). insub (); why is the compilation error?
A:"Loss of subclass Extension". The compiler only knows that new sub (). Me () is of the sup type, and sup does not have subclass-specific methods.
New sub (). Me () can only call. insup ().
(Sub) New Sub (). Me (). insub (); // downward styling
③ Suppose sub2 extends sup, there is a method of insub2. (Sub2) New Sub (). Me (). insub2 (); what will happen?
A: The compilation is valid, but thrown during runtime.Java. Lang. classcastexception: Typesystem. sub cannot be cast to typesystem. sub2
Static type check and inheritance