Java basics 8: java Basics
Java basics Bayi. Features of constructor in the Child parent class 1.1 why did the parent class run when accessing the subclass constructor when the child class constructs the object?
The reason is that there is a default implicit statement in the first line of the subclass constructor. Super ();
There is actually a return statement in the constructor;
Subclass instantiation process: All constructors in the subclass access the constructors with null parameters in the parent class by default.
Note:
In fact, you only need to remember that the sub-class constructor has a super (); by default. So when the parent class has parameters, super should also have parameters.
Constructor cannot be overwritten because the names are different.
The constructor cannot be inherited, so we have super.
Super is only saved by java. It is not required for this sentence. In fact, we write all the sub-class constructors.
1.2 Why access the constructor in the parent class during subclass instantiation?
This is because the subclass inherits the parent class and obtains the content (attributes) of the parent class. Therefore, before using the content of the parent class,
First, you need to check how the parent class initializes its own content.
Therefore, when constructing an object, the subclass must access the constructor in the parent class.
The super () Statement is added to the constructor of the subclass.
If no null parameter constructor is defined in the parent class, the constructor of the Child class must use super to explicitly call the constructor.
The constructor in the parent class.
However, it is guaranteed that other constructors in the subclass will certainly access the constructors of the parent class.
Note: The supre statement must be defined in the first line of the subclass constructor. Because the initialization of the parent class must be completed first.
When this class constructor is called by subclass constructor,
So super is gone, because both super and this can only define the first line. So there can only be one.
There is always a constructor In the subclass that calls the parent class constructor.
All classes inherit the object class, And all classes have super ();
The constructor has the same permissions as this class. This class is public, and the constructor is also the default public.
1.3 initialization of sub-parent Variables
First, execute the subclass constructor, then execute the parent class constructor to initialize the variables, and then initialize the display of the subclass variables.
The constructor of the parent class completes initialization, and then initializes the sub-class function.
Subclass has the show method. When the parent class constructor is used, show runs the subclass because it is the subclass object.
1 class Fu 2 3 {4 5 Fu () 6 7 {8 9 super (); 10 11 show (); 12 13 return; 14 15} 16 17 18 19 void show () 20 21 {22 23 System. out. println ("fu show"); 24 25} 26 27} 28 29 class Zi extends Fu30 31 {32 33 int num = 8; 34 35 Zi () 36 37 {38 39 super (); 40 41 // --> when the parent class content is initialized through super, the member variables of the subclass do not display initialization. After super () parent class initialization is complete, the member variables of the subclass are displayed and initialized at 42. 44 45 46 47 System. out. println ("zi cons run .... "+ num); 48 49 return; 50 51} 52 53 void show () 54 55 {56 57 System. out. println ("zi show... "+ num); 58 59} 60 61} 62 63 class ExtendsDemo564 65 {66 67 public static void main (String [] args) 68 69 {70 71 Zi z = new Zi (); 72 73 z. show (); 74 75} 76 77}
Instantiation Diagram
1.4 Object Instantiation process:
An Object Instantiation process:
Person p = new Person ();
1. JVM reads the Person. class file in the specified path and loads it into the memory,
The parent class of Person will be loaded first (if there is a direct parent class ).
2. Open up space in the heap memory and allocate addresses.
3. In the object space, default Initialization is performed on the attributes of the object.
4. Call the corresponding constructor for initialization.
5. In the constructor, the first line first calls the constructor in the parent class for initialization.
6. After the parent class is initialized, display and initialize the attributes of the subclass.
7. initialize the subclass constructor.
8. After initialization, assign the address value to the referenced variable.
Initialize by default, construct initialization, and display Initialization
Ii. disadvantages of final keyword 2.1 inheritance
Break Encapsulation
Solution: add final before the class so that you will not inherit it
2.2 final Functions
Final keywords:
1. final is a modifier that can be used to modify classes, methods, and variables.
2. The final modified class cannot be inherited.
3. The final modification method cannot be overwritten.
4. The final variable is a constant and can only be assigned once.
Why use final to modify variables. In fact, if a program data is fixed,
You can use this data directly, but it has poor readability. Therefore, the data is named.
The value of the variable name cannot be changed, so the final value is fixed.
Writing rules: all the letters of a constant are uppercase, multiple words are separated.
If it is modified by final, it becomes final and cannot be inherited.
Iii. abstract classes
Abstract class:
Abstract: general, fuzzy, and incomprehensible! Not specific.
Features:
1. When a method is declared to be unimplemented, the method is an abstract method and needs to be abstract modified.
Abstract methods must be defined in abstract classes. This class must also be abstract.
2. abstract classes cannot be instantiated. Why? It is meaningless to call the abstract method.
3. An abstract class must have its subclass covering all abstract methods before it can be instantiated.
Otherwise, this subclass is still an abstract class.
1. Is there any constructor in the abstract class?
Yes. It is used to initialize the subclass object.
2. can abstract classes not define abstract methods?
Yes. But it is rare that the purpose is not to allow this class to create objects. AWT adapter objects are of this type.
Methods In this class usually have method bodies, but there is no content.
Abstract class Demo
{
Void show1 ()
{}
Void show2 ()
{}
}
3. Cannot abstract keywords coexist with those keywords?
Private
Static
Not final
4. Similarities and Differences between abstract classes and general classes.
Similarities:
Abstract classes and general classes are used to describe things, and members are set internally.
Different:
1. The general class has enough information to describe things.
Abstract classes may not provide sufficient information to describe things.
2. abstract methods cannot be defined in a general class. Only non-abstract methods can be defined.
Abstract classes can define abstract methods and non-abstract methods.
3. General classes can be instantiated.
Abstract classes cannot be instantiated.
5. Must an abstract class be a parent class?
Yes. The subclass can be instantiated only after its method is overwritten by the subclass.