Member variables:
First look at this piece of code:
The parent class. Class Fu{int num = 3;} Class Zi extends Fu{int num = 4;void Show () {System.out.println ("num =" +this.num);}} Class Extendsdemo {public static void main (string[] args) {Zi z = new Zi (); Z.show ();}}
See how it works from a single graph:
The picture is a bit messy, take a look at the order. In code, the use of super and this is very similar, this represents a reference to this class of objects, and super represents the memory space of the parent class.
member functions:
"Characteristics of member functions in the child parent class"
Special case: An identical function is defined in the child parent class.
Run Result: The function of the subclass is running.
In the child parent class, this is another feature of the function: override (override, overwrite, duplicate)
When does rewriting work? Example:
Describe the phone. Class Phone{int number;//on the phone. void Call () {}//caller ID is displayed. void Show () {SOP ("phone Number:") +number);}} Phone p = new phone ();p. Show (); With the phone upgrade, only show the number is not happy, want to display the name, photos. Modify the source code, although laborious but can be solved, not conducive to later maintenance and expansion. For ease of expansion. New feature is not new phone to have it? Individual encapsulation is described separately. The new phone is also one of the phones. Inherited. Gets the functionality in the parent class directly. But the new phone's feature has changed. Need to be redefined. So is it appropriate to define a new feature? NewShow, for example, is inappropriate because the parent class is already defined in the future, and the subclass does not need to redefine the new functionality at all. You can use it directly. If the child class has different explicit functionality. You need to keep it explicit, define the contents of the subclass: This is the app for rewriting! Class Newphone extends phone{string name; String picpath;//the picture path. void Show () {//sop ("phone number"); Super.show ();//If you also need some of the features of the parent class, you can call through super. System.out.print ("name" +name); System.out.print ("photo" +picpath);}}
"Overriding (overriding) Considerations"
1, the subclass overrides the parent class and must be guaranteed to be greater than or equal to the permissions of the parent class.
Fu:
private void Show () {}
Zi:
public void Show () {}
2, static override static.
Note: It must be exactly the same: the function's return value type function name parameter list is identical.
Summary
When a class is one of the other classes, you can extend the functionality by inheriting it.
Overrides are used when the feature content from the parent class requires a special definition of the subclass.
+ + "Characteristics of constructors in sub-parent classes"
When the child parent class has constructors, the result is:
Fu Constructor Run
Zi Constructor Run
The constructor of the parent class is executed before the constructor of the subclass is executed.
"Why is that?" 】
Because the first line in all the constructors of a subclass has an implicit statement super (); The default call is the constructor of the null argument in the parent class.
"Why does a constructor in a subclass have an implicit super ()?" 】
Cause: Subclasses inherit content from the parent class, so when the subclass initializes, it must first go to the parent class to perform the initialization action of the parent class.
To make it easier to use the content in the parent class.
Summary
When there is no empty argument constructor in the parent class, the constructor for the subclass must be isomorphic to the constructor in the parent class to be accessed by the Super statement that is displayed.
This is the legendary subclass instantiation process.
"Details of the instantiation process"
1, if the first line of the subclass's constructor writes this call to the other constructors of this class, then the statement of super calls the parent class is gone, because this () or super () can only be defined in the first row of the constructor, because the initialization action is performed first.
2, is there an implicit super in the parent constructor?
Yes. Remember: As long as the constructor is the default, the first row is super ();
Who is the parent of the parent class? Whose constructor is the super call?
The Java system is designed to define a parent class object for all objects,
Summary
The constructors in the class have the default first row of the implicit super () statement, which accesses the constructor in the parent class.
So the parent class's constructor can either initialize its own object or initialize its own subclass object.
If the default implicit super statement does not have a corresponding constructor, the constructor must be explicitly called in the constructor through the form of this or super.
Problem
Can 1,this statements and Super statements appear in the same constructor? No, because the first line must be defined.
2, why do you define it in the first line? Because the initialization action should be performed first.
member variables and functions in the child parent class of Java inheritance