子类不能调用父类构造器

子类不能调用父类构造器


 1/**
 2 * Author: wangy325
 3 * Date: 2024-07-21 01:27:52
 4 * Description: 子类不能调用并未实现的构造器
 5 */
 6
 7public class InitSubClass {
 8
 9    public static void main(String[] args) {
10        Extended ext = new Extended();
11        ext.pubMethod();
12
13        // 父类有带参构造器,子类并没有实现
14        // Extended ext2 = new Extended("xxxx");
15    }
16}
17
18/***
19 * Base no-arg constructor invoked~
20 * class attr: Base attr
21 * private attr: Base private attr
22 */
23
24class Base {
25
26    static String attr = "Base attr";
27    private String private_attr = "Base private attr";
28
29    public Base() {
30        super();
31        System.out.println("Base no-arg constructor invoked~");
32    }
33
34    public Base(String args) {
35        this.private_attr = args;
36    }
37
38    private void bMethod() {
39        System.out.printf("class attr: %s\nprivate attr: %s\n",
40                attr,
41                private_attr);
42    }
43
44    public void pubMethod() {
45        bMethod();
46    }
47}
48
49class Extended extends Base {
50    // do nothing
51    // but still has a implicit constructor
52}