JavaSE face questions: class and instance initialization Initialization

Class initialization process

1, to create an instance of a class you need to load and initialize the class

   Class main method where the need to load and initialize

2, a subclass to initialize need to initialize the parent class

3, a class initialization is performed <clinit> () method

<clinit> () method from the displayed static class variable assignment code and the static block of code consisting of

Class variable display block assignment code and static code execution order from top to bottom

<Clinit> () method is executed only once

 

Examples of the initialization process

1, initialization is executed example <init> () method

<Init> () method may have a plurality of overload, there are several constructors have several <init> method

<init> () method from the displayed code is assigned non-static instance variables and non-static code block , the corresponding configuration code consisting of

Non-static instance variables are displayed and non-static assignment of code block execution order from top to bottom , while the corresponding final execution code constructor

Each create an instance, call the corresponding constructor is performed corresponding <init> method

first line <init> method is super () or super (a list of instances ), i.e., corresponding to the parent <init> method

(Super write or do not write at all, in the subclass constructor will call the parent class's constructor)

 

Override method of rewriting

1, which methods can not be overridden

final method

Static method
private subclass like invisible Method

2 polymorphism objects

In fact, the previous non-static method has a default target this

In this configuration (or <init>) which indicates that the object being created, because it is created Son object,

test () is a subclass overrides execution code (object-oriented polymorphism)

 

Advanced Requirements

Difference Override (rewritable) and Overload (overload) of

Rewriting only exists in the child and parent class, the class of the overload exists in a

Specific differences are as follows: 
First, the override (the override)

override is rewritten (overwritten) a method to achieve different functions. Is generally used when the subclass inherits the parent class, override the parent class (re-implement). 
Rewrite Rules (coverage) of: 
1, rewrite method of parameter list must be exactly the same as the method to be overridden , but otherwise could not be called to rewrite overloaded. 
2, rewrite method of access modifier must is greater than the access modifier is rewritten method (public> protected> default> private  ).
3, override the return value must be consistent with the method returns to be rewritten; 
4, a method override exceptions thrown exception must be rewritten methods consistent thrown, or is a subclass ; 
5, the method can not be rewritten to private, or in which the new subclass only defines a method, there s no override it. 
6, static methods can not be overridden method is non-static (compile error).

Two, overload is overloaded, the method is generally implemented in a number of overloaded for a class, the same names and forms of various parameters of these methods. 
Overload rules: 
1, only, forms of implementing different parameters when using overloaded by the same method name . Different types of parameters may be different parameter types, the number of different parameters, a different sequence parameter ( parameter types must be different ); 
2, can not access the return type, the thrown exception overloaded; 
3, method the exception type and number will not impact on overloaded; 
concept of polymorphism is more complex, there are a variety of multi-state sense, an interesting but not rigorous argument is: Inheritance is a subclass use the parent class, and polymorphism the method used is the parent class subclasses. 
Generally, we use polymorphism is to avoid overloading caused a large number of the parent class code is bloated and hard to maintain.

 

 

Override override the requirements?

1, subclass override method must have the same name, and parameter list of the parent class is rewritten

2, Subclasses override the return type of the method is greater than the parent class is not overwritten return type

3, access method using a subclass overrides not be less than the parent access method to be rewritten

Subclasses can not override the parent class method declared private rights

4, the subclass exception thrown exception is not greater than the parent class is rewritten method

 

Learn "JVM Virtual Machine Specification" in the description of the <clinit> and <init> method, invokespecial instruction

 

Guess you like

Origin www.cnblogs.com/helloworld0903/p/11462875.html