final keyword, polymorphism (notes)


content


《Thinking in java》读书扩展

 作者:淮左白衣

 写于 2018年4月16日17:54:35

protected

Used to decorate , the access rights of the representative domain are: 包权限or 不同包,但是是子类;


fianl

值得好好讲下,并不是简单的一个关键字
  • final modifier constant

    As long as this constant is substituted 计算式, it will be calculated at compile time to reduce the burden on runtime. ( Only for the calculation formula of basic data type, it works )

  • final decorated reference

    There is a place to mention this, it is only the object pointed to by the reference, once it is determined, it can no longer be modified to point to other objects . But the properties of the object itself can be changed at will;

  • blank final

    就是被申明为final,但是未赋值的域, called 空白final; but before using it, it must be assigned a value; in
    this way, it can be assigned flexibly, but its immutable characteristics are maintained;

  • final parameter

    It is a method parameter, which is modified by final; like a final reference, it cannot be changed to point to other objects;

  • final method

    The modified method can only be inherited, but cannot be overridden; the method modified as final actually uses early binding, so in ancient times ( JDK1.2、1.3?时期), this can improve efficiency;

  • final class

    The final class cannot be inherited. The methods of the final class are implicitly set as final, but the domain, which is not final, can be set according to your own wishes;


A privatemethod

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");
    }
}

There is no relationship between them; they just happen to have the same name; they cannot behave polymorphically;

方法的覆盖和向上转型, only for methods that can be inherited by subclasses in the parent class; therefore, there is no upcasting or overriding between them, and there is no polymorphic behavior;


Early binding in java

We all know that java uses late binding;

However, in fact, in some places in java, pre-binding is also used; for example: staticmethods, finalmethods; these two methods are pre-bound ;

Since it privateis an implicit finalmethod, the privatemethod is also early bound;

By the way, the constructor is also early binding, because, in fact, that 构造器is an implicit staticmethod;


immune polymorphism

静态方法and do not produce polymorphic behavior;

Any operation on is 编译时期completed at , at this time, the compiler thinks that the parent class refers to the object of the parent class , so there is no possibility of polymorphism at all;

The reason why there is no polymorphism in static methods is: 静态方法Yes 前期绑定, at this time, the compiler simply believes that the parent class reference points to the parent class object, naive!

For them, it is compile to see left, run to see left ;

class A{

    public int a = 1 ;
    private void haha(){
        System.out.println("A");
    }

    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 ;都是输出父类的。

Who gets executed first, 构造器or 初始化?

From the code point of view, there will be misunderstandings, and the first code of the constructor is executed first;

这里写代码片

In fact, from 虚拟机the level of , the initialization is performed first, and then the constructor is executed ;

This involves that when the class loader loads the class, it will load the class information into the method area . When the virtual machine checks the type information of the class, if it finds a parent class, it continues to load the class file of the parent class. , if there is no parent class, start to initialize the class, and then execute the constructor; then return layer by layer, which is seen from the virtual machine level;

However, from the 代码层次perspective , it gives us the feeling that the constructor is executed first, and judgment is made to see if the first sentence of code implicitly calls the parent class constructor. If so, jump to the parent class constructor and continue. Execute this logic; until there is no parent class, then initialize, after the initialization is complete, continue to execute the remaining code in the constructor ;


covariant return type

In the JDK1.5future, in the method overridden by the subclass, the return value type of the subclass method can be the subclass of the return value of the method of the parent class ;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324503654&siteId=291194637