Statement: The material used in this column is written by the VIP students of the Kay Academy, who has the right to be anonymous and has the final right to interpret the article; The Edith Academy is designed to promote students to learn from each other on the basis of public notes.
Rule: 1. When you create a subclass object, the child class is first out of the parent class.
2. When invoking a method, the child class is considered first, and the subclass is not called the parent class.
Code angle, first create the parent memory, then create a subclass of memory, the subclass is superimposed on the parent class memory, when the call first consider the above (subclass) a layer of memory, consider the following (the parent class) of memory, so first out of the parent class and then out of the subclass is an overlay effect.
Super keyword: Super braces are written on the constructor and are used to select a constructor of the parent class to build.
Writing the Super keyword in the usual method means a method to invoke the parent class.
Example: Apply for T1, T2 class, T2 inherit from T1.
Process: The code executes to T2 's main method, then T2 T2 new T2 and passes the "starry sky" content to the T2 constructor. Passed in after the Super keyword declaration passed the name to the T1 constructor in the Name,t1 constructor, the name is also passed to the T1 property, the T1 attribute is hosted in the "Starry Sky", T2 called the Get Name method is the parent class get name method, and finally the "star" Print it out.
Final:
modifier on the variable, the constant variable. The name is specified, and the names need to be all uppercase.
Modifier on the property, the property becomes constant. 1. (public static final String name= "KK";)
2. It is not stored together with object memory or class memory, and it is dedicated to a constant layer of storage.
Modifiers on methods, subclasses cannot override this method.
Decoration on a class, a class cannot be inherited, and a subclass cannot be derived.
Final decoration on class: not inheritable.
Final modification on the method: Error!
Final decoration on attributes, final is a constant, constants are immutable, modifiers are added to the property, and static members can be accessed.
Strange question:
To build the object step: 1. Loading properties, methods
2. Attribute assignment, loading instance block
3. Build constructor Call
Explanation: New Test2 loads the properties and methods of the Test1, and then the Test2 property method is loaded but the property has not been assigned a value. It then loads the instance block and assignment of the parent class, when the Test1 constructor is built to begin the call. Test2 overrides the Test1 method, Test1 method calls to the Test2 method. The properties of the Test2 have not been assigned, and the Test2 method directly prints the i. The final print result is 0. (Test does not perform attribute assignment, comfort is 0)
Example 1: Send an example by SMS.
1. Write the phone Code parent class, the class inside get, set settings mobile phone number, verification code attributes, send SMS method.
2. Apply subclass inheritance of different platforms with phone Code, subclass rewrite send SMS method. Depending on the platform, write different ways to send text messages.
3. Apply for a text message to send a comprehensive class, SMS send a comprehensive class can be called on a platform.
Example 2: Chef class
1. The parent class writes the attributes and methods common to the chef.
2. Create a local chef, their property method inherits from the parent class, depending on the cuisine different from the way they cook, overriding the parent class method. Specification
3. You can rewrite the method to re-overwrite the master. Call the time to call the Sichuan Master 2 method.
Summary: Two uses of inheritance:
1. Derivation, derivation is the continuous reuse and upgrade, can also be said to be patched, out of the new product.
2. Specification, the specification is to use the parent class to apply the subclass of the method, let subclasses to rewrite these methods.
Java Basics-Object oriented (inheritance application)