"Java programming ideas" polymorphic interface

Up transformation
  is defined: the reference to an object of practices regarded as its reference base class is called up transformation
method call to bind
  a method call associated with a method body is called binding.
  Early binding: Binding conducted before the program execution is called early binding, early binding is the default binding, java
  late binding (dynamic binding or runtime binding): be tied to the type of the object at runtime set. In addition to the static method and the final method, all other methods are late binding in java, that is, under normal circumstances, we need not determine whether it should perform late binding, it will happen automatically.
Constructors and polymorphism

  Constructor call sequence:

  (1) calls the base class constructor, this step will be constantly repeated recursion, starting with the root structure of this hierarchy, followed by the next layer derived class, the derived class down to the lowest

  (2) initialization method is called a member of the declaration order.

  (3) call body derived class constructor    

  Parent class (static variables, static initialization block)> subclass (static variables, static initialization block)>
  parent class (variables initialization block)> parent class constructor> subclass (variables initialization block)> subclass constructor. (Variables initialization block order of definition and initialization)    
acts inside the polymorphic method constructor

  Hierarchy of constructor calls brings an interesting dilemma, if a dynamically bound method inside a constructor call is being constructed object, what would happen? It is well known within the general approach, dynamic binding at runtime call is determined, because the objects can not know it belongs to the class where the method, or is derived class of that class. If a dynamically bound method inside a constructor calls, you should use that method definition after being overwritten. This call results have been very difficult to predict, because the method covered will be called before the object is fully constructed, it may cause some errors difficult to find.

  

 1 public class Test {
 2 
 3     public static void main(String[] args) {
 4         new RoundGlyph(5);
 5     }
 6 }
 7 class Glyph{
 8     void draw(){ System.out.println("Glyph.draw()"); }
 9     Glyph(){
10         System.out.println("Glyph before draw()");
11         draw();
12         System.out.println("Glyph after draw()");
13     }
14 }
15 class  RoundGlyph extends Glyph{
16     private int radius = 1;
17     RoundGlyph(int r){
18         radius = r;
19         System.out.println("RoundGlyph.RoundGlyph(), radius = " + radius);
20     }
21     void draw(){ System.out.println("RoundGlyph.draw(), radius = " + radius); }
22 }

  Code running results:

  

   The result is not the result you expected and not the same, hey, do not worry, we have to analyze how to become such a code. In the first new main method in a RoundGlyph class, then the class loader found that it inherits the Glyph class, then it will go load the Glyph class constructor run Glyph class and call draw () method in the constructor, but this method is covered in a derived class, so this time will call the subclass draw () method, pay attention! The key to the code runs to this time just load the subclass and not initialized to subclass radius variable current value of radius just java default an initial value assigned, when the parent run draw () method when its value is only an initial value, so this time will output 0.

  So we come to a surprising conclusion: When we call the base class constructor within a certain method, and the method is exported covered by class, then call the method is a method in the derived class instead of the base class itself .

  For the preceding initialization sequence we should correct it:

  (1) Before anything else happens, the storage space allocated to the object is initialized to binary zero

  (2) calls the base class constructor

  (3) initialization method is called a member of the order of declaration

  Constructor body (4) in the derived class calls

  Finally: Writing the constructor has a valid criterion: the simplest way possible with the object enters the normal state, if possible, avoid calling other methods.

Abstract classes and abstract methods

  Abstract class: a common interface to establish a basic form, as an expression of the common part of all exported classes

  Abstract method: The method is not only a method body statements.

  The method comprising abstract class called abstract class, if a class contains one or more abstract method, then the class being defined must abstract.

  Interface: interface keywords make abstract concepts more step forward, abstract keyword allows people to create one or more do not have any method defined in the class, but did not provide any corresponding specific implementations that are of this class successor created. The interface keyword produces a completely abstract class, it does not provide any specific implementation, the interface provides only a form, does not provide any specific implementation.

  

Guess you like

Origin www.cnblogs.com/zhangchu/p/12638434.html