In Java, constructors cannot be overridden. This is because: the constructor is used to initialize the state of the object, and overwriting will destroy the initialization of the object; the constructor is the unique identifier of the class, and overwriting can cause confusion; the alternative solution is to use the superclass constructor or initialization block for object initialization.
Overridability of constructors in Java
In Java, Constructors cannot be overridden .
Reason:
Alternative solution:
Although the constructor cannot be overridden, object initialization can be modified in a subclass in the following way:
super()
keyword to call the constructor of the superclass to execute the initialization logic of the parent class. The above is the detailed content of Can constructors be overridden in Java? Why?. For more information, please follow other related articles on the PHP Chinese website!