The relationship between Java study notes 08 classes

final is the final class and cannot be inherited.
The construction method first executes the construction method of the parent class, and then executes the construction method of the subclass.

Below I simply wrote three classes to verify the execution order of the constructor


public class A1 {
    
    
     public int pf(int i){
    
    
         System.out.println("Parent....pf");
         return i*i;
     }

     public A1(int i){
    
    
         System.out.println("父类构造A1(int i)");
     }
     static {
    
    
         System.out.println("父类构造A1-static");
     }
    {
    
    
        System.out.println("父类构造A1-init");
    }
}

public class B1 extends A1{
    
    


    public B1(int i) {
    
    
        super(i);
        System.out.println("A1的儿子,子类构造B1(int i)");
    }

    /**
     * 方法的重载
     * 当参数,返回值相同时是重写
     */
    public double pf(double d){
    
    
        return d*d;
    }
    static {
    
    
        System.out.println("A1的儿子,子类构造B1-static");
    }
    {
    
    
        System.out.println("A1的儿子,子类构造B1-init");
    }



}

public class C1 extends B1{
    
    
    public C1(int i) {
    
    
        super(i);
        System.out.println("B1的儿子,子类构造C1(int i)");
    }



    static {
    
    
        System.out.println("B1的儿子,子类构造C1-static");
    }

    {
    
    
        System.out.println("B1的儿子,子类构造C1-init");
    }
}

The execution result is shown in the figure below

insert image description here
1. Execute the static code of the parent class first, and then execute the static code of the subclass.
2. Execute the initialization block and construction method of the parent class, and then execute the subclass.
If instantiated again, the static block is no longer executed.

abstract class

1. When an abstract class is declared, abstract class(), class name {}

2. Abstract classes cannot be instantiated directly.

3. An abstract class can have an abstract method abstract void show(); there can be no method body

4. Abstract methods cannot be modified with private.

5. Abstract classes are more like a programming specification.

6. A class with abstract methods must be an abstract class. An abstract class can have abstract methods, ordinary methods, or no abstract methods.

Guess you like

Origin blog.csdn.net/xxxmou/article/details/129071451