In Java Constructors are not polymorphic.
When you are dealing with already constructed class, you could be handling a declared type of the object, or any of its sub classes.
In Java a constructor is always called on the specific type,eg new String(). A hypothetical subclasses have no role in this.
You essentially do inherit the constuctors in the sense that you can simply call super if and when appropriate, it’s just that it would be error prone for reasons others have mentioned if it happened by default. The compiler can’t presume when it is appropriate and when it isn’t.
The job of the compiler is to provide as much flexibility as possible while reducing complexity and risk of unintended side-effects.
Author
Posts
Viewing 2 reply threads
You must be logged in to reply to this topic. Login here