About the final keyword java

  protected

  Used to modify the domain, the domain access is representative of: the package or in different packages rights, but is a subclass;

  final modification of the constants

  As long as the calculated constant is substituted into the formula, at compile time, calculations will be performed to reduce the burden of running. (Only basic data types calculation formula work)

  final modification of reference

  There is a need to say this place down, but pointed reference to the object, once established, would no longer be modified to point to another object. However, the properties of the object itself, can be freely changed;

  Blank final

  It is to be declared as final, but unassigned domain, called a blank final; but before use, it must be assigned;

  This allows the flexibility of its assignment, but it maintains its properties can not be changed;

  final parameters

  It is the method parameters, is the final modification; references with the same final, you can not change it to point to other objects;

  final method

  The modified method can only be inherited, but can not be overwritten; modified to final approach, in fact, is to use the early binding, therefore, do so before you can enhance efficiency;

  final class

  final class can not be inherited, final class methods, are implicitly final set, but the domain, not final, may be set according to their wishes;

  Private sub parent class method with the same name

  public class test {

  public static void main(String[] args) {

  A a = new B();

  a.haha(); ; // error

  a.hehe (); ; // OK

  }

  }

  class A{

  private void haha(){

  System.out.println(“A”);

  }

  public void hehe(){

  System.out.println("A");

  }

  }

  class B extends A{

  private void haha(){

  System.out.println(“B”);

  }

  }

  Is no relationship between them; just so happens that the same name; they are polymorphic behavior can not occur;

  Coverage and transitional-up approach, the method is only for the parent class can be a subclass inherits methods; therefore, there is no transition between them upward, covering the argument, there is no polymorphic behavior;

  Early binding of java

  As we all know, java is to use late binding;

  But, in fact, in some places in java, it can also be used early binding; for example: static methods, final methods; both methods are early-bound;

  Because private final method is implicit, therefore, private methods are early bound oh;

  By the constructor it is early binding, because, in fact, that the constructor is implicit static method;

  Immune polymorphism

  Static fields and methods are not generated polymorphic behavior;

  Any operation of the domain are done at compile time, the compiler at this time that the object is referenced by the parent class's parent class, and therefore, do not feasible polymorphism exists;

  Polymorphic reason static methods, there are: Static methods are early binding, then the compiler, is simply that, a reference point to the parent class is the parent class object, naive!

  For them, the compiler look left, look left to run;

  class A {

  public int a = 1 ;

  private void haha(){

  System.out.println("A");

  } Wuxi which hospital gynecological good http://www.bhnfk.com/

  public void hehe(){

  System.out.println("A");

  }

  public static void aa(){

  System.out.println("aa");

  }

  }

  class B extends A{

  public int a = 2 ;

  private void haha(){

  System.out.println(“B”);

  }

  public static void aa(){

  System.out.println("bb");

  }

  }

  public class test {

  public static void main(String[] args) {

  A a = new B();

  System.out.println(a.a);

  a.aa();

  }

  }

  // output: 1 aa; parent class are output.  


Guess you like

Origin blog.51cto.com/14335413/2405727