Inheritance in Java

inherit

The three major characteristics of object-oriented computer programming language:
inheritance, encapsulation, and polymorphic
inheritance : that is, subclasses inherit everything except the construction method of the parent class, which can increase the code reuse rate.
The implementation syntax is:

   class 子类  **extend**  父类{
   //子类方法
   } 

Note and characteristics of inheritance:

  • Use extends to specify the parent class.

  • A subclass in Java can only inherit one parent class, Java is single inheritance or multi-level inheritance . (And languages ​​such as C ++ / Python support multiple inheritance).

  • The subclass inherits all public fields and methods of the parent class.

  • The private fields and methods of the parent class are not accessible in the subclass.

  • The instance of the subclass also contains the instance of the parent class. You can use the super keyword to get a reference to the instance of the parent class.
     Super. Member method-> call the parent class member method
     Super. Member properties-> call the parent class into the original Attribute
     Super (parameter)-> call the constructor of the parent class ( must be placed in the first line )

  • Sealed classes modified by fianl cannot be inherited .

  • Four types of access rights for fields and methods in Java: ( access rights are reduced from top to bottom )
     public: access can be made inside and outside the
     class Classes can be accessed, other classes cannot access the
     default (package access permissions, generally default to it): classes within the class and the same package can access, other classes can not access
     private: only the class can access, other classes can not access

  • When instantiating subclass objects in Java, static code blocks can only be initialized once.
     1. That is, if the static code blocks of the parent class and the current class are not loaded outdated, the execution order is:
       static initialization block of parent class-> static initialization block of child class-> non-static initialization block of parent class-> parent class construction Method-> subclass non-static initialization block-> subclass construction method
     2. If the static code block of the parent class and the current class has been loaded, the execution order is:
       parent class non-static initialization block-> parent class Constructor-> subclass non-static initialization block-> subclass constructor (static code block will not be loaded)

public class Parent {
    static {
        System.out.println("父类静态初始化块");
    }
    {
        System.out.println("父类非静态初始化块");
    }
    public Parent() {
        System.out.println("父类构造方法");
    }
    //方法与类名同名
    public int Parent() {
        return 0;
    }
    public static void main(String... args) {
        //第一次创建时类还没有被加载,会执行静态初始化块
        Child c = new Child();
        System.out.println();
        //第二次创建时类已经被加载,只执行非静态初始化块与构造方法
        Child d = new Child();
    }
}
class Child extends Parent {
    public Child() {
        System.out.println("子类构造方法");
    }
    static {
        System.out.println("子类静态初始化块");
    }
    {
        System.out.println("子类非静态初始化块");
    }
}
运行结果:
父类静态初始化块
子类静态初始化块
父类非静态初始化块
父类构造方法
子类非静态初始化块
子类构造方法

父类非静态初始化块
父类构造方法
子类非静态初始化块
子类构造方法

Related Questions on Niuke.com

Published 5 original articles · praised 7 · visits 275

Guess you like

Origin blog.csdn.net/qq_45611822/article/details/102989656