java中实例化子类时会自动调用父类构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18415388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:25:29  来源:igfitidea点击:

parent class constructor is called automatically when child class is instantiated in java

java

提问by pavinan

abstract class A {
    abstract void method();
}

class B extends A {
    B() {
    }

    void method() {
    }
}

class C extends B {
    C() {
    }
}

When I instantiate class C in main, it automatically calls the constructor for B (parent class). Is that normal or am I doing something wrong?

当我在 main 中实例化类 C 时,它会自动调用 B(父类)的构造函数。这是正常的还是我做错了什么?

采纳答案by Prasad Kharkar

There is nothing wrong, there is implicit call to superconstructor.

没有错,隐式调用super构造函数。

You haven't written any constructor for class Cso default constructor will be provided by compiler which will be.

您尚未为类编写任何构造函数,C因此编译器将提供默认构造函数。

C(){
  super(); 
 }

If default constructor is provided, then there is a call to super(). In your case, C extends Bso constructor of Bgets called.

如果提供了默认构造函数,则调用super(). 在你的情况下,C extends B所以构造函数B被调用。

If you do not a class with any other class, then also by default it extends Objectclass. So Objectclass constructor will get called.

如果你没有一个类与任何其他类,那么默认情况下它也会扩展Object类。所以Object类构造函数将被调用。

回答by Lokesh

First line of each constructor in java calls superconstructor, thats how java works. You should read about it.

java中每个构造函数的第一行调用super构造函数,这就是java的工作原理。你应该阅读它。

回答by Juned Ahsan

If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body implicitly begins with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments.

如果构造函数体不以显式构造函数调用开始,并且被声明的构造函数不是原始类 Object 的一部分,则构造函数体以超类构造函数调用“super();”隐式开头。,调用其直接超类的构造函数,不带参数。

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.7

http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.7

回答by Patricia Shanahan

When you instantiate a C there will be constructor calls for C, B, A, and Object. A C has to be able to behave as any direct or indirect superclass, and has the fields for all of them. The job of a class X constructor is to make the object being initialized capable of working as an X.

当你实例化一个 C 时,会有对 C、B、A 和 Object 的构造函数调用。AC 必须能够像任何直接或间接超类一样工作,并且拥有所有这些超类的字段。类 X 构造函数的工作是使被初始化的对象能够作为 X 工作。

If there is no declared constructor, the compiler creates a parameterless constructor, so every class does have at least one constructor. If a constructor that is not the Object constructor does not start with a "this" or "super" constructor call, the compiler treats it as starting with "super();", a call to a parameterless constructor for the immediate superclass.

如果没有声明的构造函数,编译器会创建一个无参数的构造函数,所以每个类至少有一个构造函数。如果不是 Object 构造函数的构造函数不以“this”或“super”构造函数调用开头,编译器会将其视为以“super();”开头,即对直接超类的无参数构造函数的调用。