子类构造函数是否会默认调用父类的无参构造函数

1 package test;
2 
3 public class TestB {
4     
5     public TestB() {
6         System.out.println("TestB的无参构造函数...");
7     }
8 
9 }
 1 package test;
 2 
 3 public class TestA extends TestB{
 4 
 5     public TestA() {
 6         System.out.println("TestA的无参构造函数...");
 7     }
 8     
 9     public TestA(int i) {    
10         System.out.println("TestA的有参构造函数...");
11     }
12     
13     public static void main(String[] args) {
14         TestA a1 = new TestA();
15         TestA a2 = new TestA(1);
16     }
17     
18 }

执行上述代码后,运行结果如下:

从上述结果得知,在TestA的有参/无参构造函数中均默认调用了父类TestB的无参构造函数,即默认执行了super()代码

 

posted @ 2017-09-07 22:04  #hanw  阅读(2123)  评论(0编辑  收藏  举报