Overriding Constructors

前端 未结 5 772
庸人自扰
庸人自扰 2021-01-12 16:12

I am greatly confused with Overriding Constructors. Constructor can not be overridden is the result what i get when i searched it in google my question is

p         


        
5条回答
  •  遥遥无期
    2021-01-12 16:30

    You cannot override the constructor as the constructor is invoked by the name of the class it constructs. How can you create some different class with the same constructor? Also, a child class does not inherit constructors from its parent.

    If the parent constructor does some important initialization, it can be called from the child constructor using super:

    class Err extends Throwable {
       Err(String message) {
           super(message); // Call the constructor of Throwable.
            ..
    

    The parent class constructor is also always called. If you yourself do not call any, a constructor without parameters is called automatically before entering a constructor of the derived class. If the parent has no parameterless constructor and you do not call any, a compile time error is reported.

提交回复
热议问题