Java Implementation of Polymorphism
Basic knowledge Review
Polymorphism, that is, overloading and rewriting. the overload occurs in a class. override occurs in the subclass, which means that the subclass overrides the method with the same name as the parent class. just learning the language doesn't have to be so clear. You only need to know how to use it. If you want to really understand some problems, it's impossible in a short time, such as interfaces, without a few years of work experience, you cannot really understand what interfaces are, or even some people work for four or five years without understanding what interfaces are, do not spend too much time on these difficult questions.
The feature of overload. The method name is the same. The return type. The parameters of the input method are different (including the number and type ).
Rewrite feature. The method name is the same, the return type, and parameters are the same and must occur in the subclass.
1. the Java language allows a type of referenced variable to reference the subclass instance, and can convert the referenced variable type.
Animal animal = new Dog (); // The reference variable animal references the instance of an Animal subclass Dog
Dog dog = (Dog) animal; // converts the Animal type to the Dog type.
Creature creature = animal; // converts the Animal type to the Creature type
Animal = new Cat (); // The reference variable animal references the Cat instance of another Animal subclass.
Note: Creature refers to Creature.
2. If you convert a reference variable to a subclass type, it is called a downward transformation. If you convert a reference variable to a parent class type, it is called an upward transformation.
3. There are various restrictions when converting the type of referenced variables. In addition, when you access the static attributes, static methods, instance attributes, and instance methods of the referenced instance by referencing variables, as well as the methods and attributes inherited from the parent class, the Java virtual machine uses different binding mechanisms.
4. The member variables and static methods are statically bound according to the type declared by the referenced variables. The instance methods are dynamically bound according to the instance referenced by the referenced variables.
For example, the following code:
Fathers f = new Sons (); System. out. println ("f. var = "+ f. var); System. out. println ("f. staticVar = "+ f. staticVar); f. method (); f. staticMethod (); the following result will be output during the runtime: f. var = FatherVarf. staticVar = StaticFaterVarSon methodStatic Father methodclass Fathers {String var = "FatherVar"; static String staticVar = "StaticFatherVar"; void method () {System. out. println ("Father method");} static void staticMethod () {System. out. println ("Static Father method") ;}} class Sons extends Fathers {String var = "SonVar"; static String staticVar = "StaticSonVar"; void method () {System. out. println ("Son method");} static void staticMethod () {System. out. println ("Static Son method");} String sonVar = null; void sonMethod (){}}