JavaSE review diary: polymorphism, javase review diary Polymorphism

Source: Internet
Author: User
Tags list of attributes

JavaSE review diary: polymorphism, javase review diary Polymorphism

/*** Rrys replacement principle: * where the parent class can be used, the subclass * What is polymorphism: * Reference of the parent class, prerequisite for * polymorphism of the object pointing to the subclass: * two classes with inheritance relationships * polymorphism purpose: * ☆☆☆code reuse * When is the polymorphism: * When assigning values **!!!!!!!!! In polymorphism, ☆☆☆override refers to the member method in particular !!!!!!!!! * What is overwriting? * indicates that a subclass has the same method as its parent class: method name, parameter list, and return value type (it has nothing to do with the modifier list) * 1. The method name is the same: different, that is, two different methods. * 2. The parameter list is the same: the parameter list is different, that is, the method is overloaded, it is not the same if the number and Data Types of the parameter list are the same. * 3 the return value type is the same. The return value indicates the function of the function (method, overwriting is intended to be more powerful and not to reduce functions. Therefore, you must perform the same * overwriting and do not have lower access permissions than the original method (reducing the permissions means less functionality) * After overwriting, there cannot be a broader exception (only exceptions get lower and lower) than the original method )**!!!!! ☆☆☆The attribute list of the parent class is generated at Compilation: that is, when javac is used ☆☆ *!!!!! ☆☆☆The attribute list of the subclass is generated at runtime: that is, when java is running ☆☆☆** there are two methods for calling the parent class and subclass: * 1 multi-state parent class references the object parent class variable name pointing to the subclass = new subclass Constructor (); * Static variables, static methods, and member variables: * When you call a method or variable using the referenced variable name, you must first find the parent class if this variable (method) exists ). it will be executed directly and will not be found in the subclass. If the parent class does not have this variable (method), an error will be reported directly (error: the symbol cannot be found) and will not be found in the subclass, because the list of attributes of the subclass does not exist yet and an error is reported during the compilation period, * member methods cannot be found in the subclass: * when the member methods are called, find them in the parent class first, if the parent class exists, go to the subclass to find it. If the subclass overrides the member method, the member method of the subclass is executed. If the subclass is not overwritten, the parent class is also executed, however, if the parent class does not have a member method, but the subclass has one, an error is reported directly (error: the symbol cannot be found) and the subclass is not found (the unique member method of the subclass is not executed ). No attribute list is available, but an error is reported during compilation. * 2 directly instantiate the sub-type variable name = new subclass Constructor (); * Static variables, static methods, and member variables: * search for the sub-classes first. If the sub-classes have this variable (method ), if the sub-class does not have this variable (method), the parent class will be searched for and the parent class will be executed. However, if neither the sub-class nor the parent class is executed, an error is reported (error: No symbol is found) * member method: * If a subclass exists and a parent class exists, the member method is executed according to the subclass. This is a method override. If the subclass does not exist, find the parent class and execute the parent class. If the child class has and the parent class does not, execute the Child class (used to execute the unique method of the Child class, which is not the child class ), if none of them exist, an error is returned (error: the symbol cannot be found) **/public class Duo_Tai {public static void main (String [] args) {// The first polymorphism parent class references the object Fu_Lei f = new Zi_Lei () pointing to the subclass ();/ /------------------ Variable ------------------------------ // The static variable int I = f. i; System. out. println (I); // output 100 is the static variable of the parent class // some subclasses of the parent class also have static variables int a = f. a; // the two variables here will not conflict because the previous int a is the f. a is the static variable System in the class. out. println (a); // output 1 is the static variable of the parent class. Why is the output not the 111 variable of the subclass? // The member variable int j = f. j; System. out. println (j); // output 200 is the member variable of the parent class // some subclasses of the parent class also have the member variable int B = f. b; System. out. println (B );// Output 2 is the member variable of the parent class. Why is it not the static variable of the subclass 2222/* // The parent class does not have a subclass? Why? Why not call subclass? Int m = f. m; // Duo_Tai.java: 48: error: the System. out. println (m) symbol cannot be found; // Why is there a member variable in the parent class without a subclass? Why not call subclass? Int n = f. n; // Duo_Tai.java: 52: error: the System symbol cannot be found. out. println (n); * // ---------------------- method ------------------------------ // static method f is not available for subclass of the parent class. m6 (); // output the static method of the parent class // some subclasses of the parent class also have static method f. m3 (); // outputs the static method of the parent class. // The member method f is not included in the subclass of the parent class. m2 (); // outputs the member methods of the parent class // some subclasses of the parent class also have member Methods f. m1 (); // output the member method of the parent class/* // Why does the static method of the parent class return an error? Why not call subclass? F. m5 (); // Duo_Tai.java: 68: Error: The symbol cannot be found. // Why does the parent class have a member method that does not have a subclass report an error? Why not call subclass? F. m4 (); // Duo_Tai.java: 71: Error: the symbol cannot be found * // -------------------------------------------------------------------- // The second type directly instantiates the multi-state subclass references the subclass object Zi_Lei z = new Zi_Lei (); // -------------------- variable ------------------------------ // The static variable int i1 = z. i; // output the static variable System of the parent class. out. println (i1); // Why is no error reported for the subclass? Instead, call the static variable int a1 = z. a; // The static variable System of the output subclass. out. println (a1); // The member variable int j1 = z. j; // output the member variable System of the parent class. out. println (j1); // some sub-classes of the parent class also have member variables int b1 = z. b; // output the member variable System of the subclass. out. println (b1); // The static variable int m1 = z. m; // The static variable System of the output subclass. out. println (m1); // Why is no error reported for the parent class? // The member variable int n1 = z. n; // output the member variable System of the subclass. out. println (n1); // ---------------------- method ------------------------------ // static method z is not available for subclass of the parent class. m6 (); // Why is no error reported for the static method that outputs the parent class? In addition, the static method of the parent class is called. // some subclasses of the parent class also have static methods z. m3 (); // static method of the output subclass // The member method z that does not exist in the subclass of the parent class. m2 (); // Why is no error reported for the member method of the output parent class? Also, call the member methods of the parent class. // some subclasses of the parent class also have the member Methods z. m1 (); // output the member methods of the subclass. Why ?!!!! ☆☆☆// The static method z of the parent class without subclass. m5 (); // static method of the output subclass // The parent class does not have a member method z. m4 (); // member method of the output subclass} class Fu_Lei {// declare a parent class static int I = 100; // The static int a = 1 does not exist in the subclass of the static variable parent class; // The static variable subclass also has an int j = 200; // The Child class of the parent class does not have int B = 2; // The Child class of the parent class also has public void m1 () {// The member variable that is overwritten (the Child class has) system. out. println ("I am a Member method of the parent class,");} public void m2 () {// No overwritten member variable (not included in the subclass) System. out. println ("I am a Member method of the parent class, not overwritten");} public static void m3 () {// static method subclass also has System. out. println ("I Am a static variable of the parent class");} public static void m6 () {// The static method System that does not exist in the subclass. out. println ("static method not available for subclass");} class Zi_Lei extends Fu_Lei {// declare a subclass inheritance (extends) parent class static int m = 3; // The static variable int n does not exist in the parent class = 8; // The member variable static int a does not exist in the parent class = 111; // The parent class also has int B = 2222; // The parent class also has public void m1 () {// The member Method System of the parent class. out. println ("I Am a subclass, I overwrite the m1 () member method of the parent class");} public void m4 () {// The member method System that is not present in the parent class. out. println ("I Am a subclass, my method, no parent class, my own");} public static void m3 () {// static method System of the parent class. out. println ("I Am a static method of the subclass, and the parent class is also");} public static void m5 () {// static method not available for the parent class System. out. println ("I Am a static method of the subclass, the parent class does not exist ,");}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.