Java polymorphic properties are statically bound (compiler binding), and methods are dynamically bound (runtime binding). At runtime, the property outputs the value of the parent class, and the method runs the method of the subclass. That is, compile and look at the left, and run and look at the right .
Java polymorphism cannot access properties and methods specific to subclasses.
Scala
Scala polymorphic properties and methods are dynamically bound. At runtime, properties and methods are all subclassed.
Java polymorphism also cannot access the unique properties and methods of the subclass.
Java code
publicclassJava02_Dynamic{
publicstaticvoidmain(String[] args){
Person02 t =newTeacher02();// Java中属性是静态绑定(编译期绑定)
System.out.println(t.name);// person// 不能访问子类特有的方法和属性// t.addr//t.sleep();// java中方法是动态绑定(运行期绑定)
t.hello();// Teacher hello}}classPerson02{
String name ="person";int age =18;publicvoidhello(){
System.out.println("Person hello");}}classTeacher02extendsPerson02{
String name ="student";
String addr ="China";@Overridepublicvoidhello(){
System.out.println("Teacher hello");}publicvoidsleep(){
System.out.println("techer sleep");}}