Package com.hanwei.service;
Class testparent{public
testparent () {
System.out.println ("I am a parameterless constructor for the parent class");
}
Public Testparent (String a) {
System.out.println ("I am a parameterized constructor for the parent class");
}
static{
System.out.println ("I am in the parent class static Block")
;
{
System.out.println ("I am in a non-static block of the parent class");
}
public class Test extends testparent{public
test () {
//super ("");
System.out.println ("I am a parameterless constructor for subclasses");
}
static{
System.out.println ("I am in a subclass static block");
Public test (String a) {
System.out.println ("I am a parameter-structured method of subclasses");
}
{
System.out.println ("I am in a subclass non-static block");
}
public static void Main (String arsg[]) {
new Test ();
}
}
For the order of execution above
1. Static blocks of the parent class
2. Sub class static Block
3. Non-static blocks of the parent class
4. The method of constructing the parent class without parameters
5. Non-static blocks of subclasses
6. Sub-class without parameter construction method
If we are new test ("1"), then the previous 1,2,3,4,5 order will be the same, and 6 will perform its own method of constructing the parameters. If there is a parameterless constructor in the parent class, the subclass will also unconditionally run the constructor of the parent class, noting that the invocation is not an inheritance and that the subclass is not able to inherit the constructor method of the parent class.
The situation is as follows
But always remember the phrase "if there are no explicit write-only constructor methods in the parent class, and only constructors with parameters, the parent class's method of constructing with parameters must be invoked before executing the corresponding construction method of the subclass, otherwise there will be an error."
1. There is no explicit constructor in the parent class, and the default parameterless constructor of the parent class is not explicitly called by the subclass constructor before execution, and it also unconditionally invokes the default parameterless constructor method in the parent class.
As follows:
Package com.hanwei.service;
Class testparent{
} public
class test extends testparent{public
static int a=0;
Public test () {
//In fact, this is super. The parameterless constructor
System.out.println ("I am a subclass of parameterless constructors") called the parent class;
public static void Main (String arsg[]) {
new Test ();
System.out.println (TEST.A);
}
2. There are only constructors with parameters in the parent class, there is no explicit parameterless constructor, and the constructor method in the subclass does not have an explicit super call to this parameter constructor of the parent class, so no matter how the call is made, there will be errors, as shown below
(1)
Package com.hanwei.service;
Class testparent{public
testparent (String a) {
System.out.println ("I am a parameterized constructor for the parent class");
}
public class Test extends testparent{public
static int a=0;
Public test (String a) {
//TODO auto-generated constructor stub
} public
static void main (string arsg[]) {
new testparent ("")///subclass does not have super Call parent class (with parameter) constructor
System.out.println (TEST.A);
}
(2)
Package com.hanwei.service;
Class testparent{public
testparent (String a) {
System.out.println ("I am a parameterized constructor for the parent class");
}
public class Test extends testparent{public
static int a=0;
Public test () {
System.out.println ("I am a parameterless constructor for subclasses");
}
public static void Main (String arsg[]) {
new Test (); The parameter constructor
System.out.println (TEST.A) of the parent class is not invoked in the subclass parameterless constructor;
(3)
Package com.hanwei.service;
Class testparent{public
testparent (String a) {
System.out.println ("I am a parameterized constructor for the parent class");
}
public class Test extends testparent{public
static int a=0;
Public test (String a) {
System.out.println ("I am a parameterless constructor for subclasses");
}
public static void Main (String arsg[]) {
new Test ("");
System.out.println (TEST.A);
}
In other words, as long as the subclass has an object, it is bound to execute a constructor for the parent class. If there is no explicit or implicit invocation, it will go wrong, because the explicit super call always constructs the first sentence of the subclass constructor, so the parent class that executes must be preceded by a subclass.
If the subclass constructor does not explicitly call the constructor of the parent class, the default (no parameters) constructor for the parent class is automatically invoked. If the parent class does not have a constructor with no parameters, and the constructor of the parent class is not explicitly called in the constructor of the subclass, the Java compiler will report an error (except for the absence of any explicit constructors in the parent class), which is also the default (no parameter) constructor for the default invocation of the parent class.
Objects are always accompanied by this new generation, and if there is no new then direct static members such as class names are generated without objects
Subclasses cannot inherit the constructor of the parent class, and if the subclass has a parameterless constructor that invokes the parent class, then a parent object is created, and then the member that you own becomes a subclass object, and now that the parent object is generated, the private member is inherited, but the subclass is not accessible, as in common entity classes, Subclasses can access and modify the member variables of the parent class through the get and set methods, but the parent class private method cannot be invoked. Private car members are hidden, but the subclasses exist in memory, but cannot be invoked.
Does the member subclass of the private modifier have inheritance?
The Netizen from Baidu answers:
The subclass inherits the parent class, and the subclass has all the properties and methods of the parent class.
program validation, the private properties and method subclasses of the parent class are not directly accessible. Of course, private properties can be accessed through the public-decorated getter and setter methods, but private methods are not.
Assuming that subclasses cannot inherit the private properties and methods of the parent class
Then: After parsing the memory, it will be found that when a subclass is instantiated, the default is to invoke the parent class's constructor to initialize the parent class, that is, to create a parent class object in memory, and then to place the parent class object's unique attributes on the outside of the subclass, which together become objects of a subclass.
So: subclasses inherit all of the properties and methods or subclasses of the parent class that have all the attributes and methods of the parent class are right, except that the private properties and methods of the parent class are not directly accessible. That is only owned, but cannot be used
The following summary text overview comes from: http://blog.csdn.net/u010442302/article/details/52052091
In Java, static can modify member variables, member methods, code blocks, and inner classes.
Static modifies a member variable to achieve the effect of a global variable (Java does not have the concept of a global variable), when a class defines a static variable, it actually applies to a memory address, and all objects of that class share the static member variable. Static variable reference mode: class name. Static variable, object name. Static variable.
Here distinguishes: static and instance variables. Static variables: Use static modifier, belong to class, as long as the class is loaded will be allocated memory space; instance variable: There is no static decoration, only objects are created, will allocate memory space, each object's instance variables are not related, reference way: only objects. Instance variables.
2.static decorated member methods, the static-decorated method is a class method that can be invoked without creating an object, rather than a static method, which can be invoked only if the object is created.
Static methods cannot use the This,super keyword, cannot call non-static member variables, are not static member methods, can access only static-decorated member variables, member methods. Because there is no static-decorated member variable, member method, the object of this class has not been created, even if it is created and cannot be determined by which object.
3.static Cosmetic code blocks, which are executed at the time the class is loaded and executed only once, are generally used to initialize static variables and invoke static methods, specifically: http://blog.csdn.net/u010442302/article/details/51365857
4.static modifies the inner class so that it can be instantiated without relying on an instance object with an external class, whereas a typical inner class requires an external class instantiation before it can be instantiated. Static inner classes cannot have the same class name as the outer class, cannot access the member variables of the external class, and can only access static member variables and static methods of the external class, including the private of the external class. Instance inner class format: Outer.Inner name = new Outer.innner ();
Java initialization, load order: Parent class static member variable and parent class static code block (who before the first call who, sequential loading), subclass static member variables and subclass static code blocks (who first call who, in order to load), the parent non-static member variables, the parent class non-static code block, Parent class constructor, subclass non-static member variable, subclass non-static code block, subclass constructor