Is Constructor Overriding Possible?

后端 未结 14 1876
时光说笑
时光说笑 2020-12-08 00:31

What i know is, the compiler writes a default no argument constructor in the byte code. But if we write it ourselves, that constructor is called automatically. Is this pheno

相关标签:
14条回答
  • 2020-12-08 01:21

    Constructor overriding is not possible because of following reason.

    Constructor name must be the same name of class name. In Inheritance practice you need to create two classes with different names hence two constructors must have different names. So constructor overriding is not possible and that thought not even make sense.

    0 讨论(0)
  • 2020-12-08 01:24

    But if we write it ourselves, that constructor is called automatically.

    That's not correct. The no-args constructor is called if you call it, and regardless of whether or not you wrote it yourself. It is also called automatically if you don't code an explicit super(...) call in a derived class.

    None of this constitutes constructor overriding. There is no such thing in Java. There is constructor overloading, i.e. providing different argument sets.

    0 讨论(0)
  • 2020-12-08 01:24

    Constructor looks like a method but name should be as class name and no return value.

    Overriding means what we have declared in Super class, that exactly we have to declare in Sub class it is called Overriding. Super class name and Sub class names are different.

    If you trying to write Super class Constructor in Sub class, then Sub class will treat that as a method not constructor because name should not match with Sub class name. And it will give an compilation error that methods does not have return value. So we should declare as void, then only it will compile.

    0 讨论(0)
  • 2020-12-08 01:25

    You can have many constructors as long as they take in different parameters. But the compiler putting a default constructor in is not called "constructor overriding".

    0 讨论(0)
  • 2020-12-08 01:31

    It should also be noted that you can't override the constructor in the subclass with the constructor of the superclass's name. The rule of OOPS tells that a constructor should have name as its class name. If we try to override the superclass constructor it will be seen as an unknown method without a return type.

    0 讨论(0)
  • 2020-12-08 01:33

    Constructors are not normal methods and they cannot be "overridden". Saying that a constructor can be overridden would imply that a superclass constructor would be visible and could be called to create an instance of a subclass. This isn't true... a subclass doesn't have any constructors by default (except a no-arg constructor if the class it extends has one). It has to explicitly declare any other constructors, and those constructors belong to it and not to its superclass, even if they take the same parameters that the superclass constructors take.

    The stuff you mention about default no arg constructors is just an aspect of how constructors work and has nothing to do with overriding.

    0 讨论(0)
提交回复
热议问题