java - Calling a private super class constructor implicitly from the subclass results in random behavior -
java - Calling a private super class constructor implicitly from the subclass results in random behavior -
package pack1; public class outer{ private outer(){} } bundle pack2; import pack1.*; public class extendedouter extends outer{ //compiler adds default no-args constructor, /* public extendedouter(){ super(){} } */ } 3rd class import pack1.outer; import pack2.extendedouter; class innerclasses{ public static void main(string[] args){ extendedouter o=new extendedouter(); } }
but hey, outer
's constructor private , expect code not compile, compile fine. @ runtime know goofed when throws illegalaccesserror
happens ! error outer() private
, don't error how 1 explain such random behavior ?
but private method invocation warned @ compile time. why not when comes implcitly added constructors? ps: compiler throw error outer() private in outer class
if explicitly add together constructor extendedouter(){}
in extendedouter
class
edit----------------------
i sorry makes no sense. indeed unpredictable , outcomes differ 1 compilation other surprise ok if doesn't work , seek switching modifier of outer constructor private
public
ok , works , compiles switch modifier private
again, compiles 1 time again ! shouldn't have, no? run , u encounter illegalaccesserror
now after sometime, cud happen doesn't compile private
showing typical message outer() not public in outer class
great . switch modifier of outer()
constructor public again, compiles ! switch private
again, compiles (whereas wasn't compiling)
try out , allow me know don't forget compile javac -cp
this question 2 questions. unfortunately, due nature of questions, these answers speculative. here questions believe want answered:
what compilation mechanics of private constructors beingness referenced auto-generated default constructors?
if create constructor of class private
, class cannot sub-classed. private
constructor effective way prevent sub-classing final
keyword; subclasses forced via super()
effort phone call unreachable constructor. because extendedouter
class references private
constructor of outer
class implicitly (via generated constructor code) compiler not seem aware attempting phone call unreachable constructor.
it seem compiler fails consider side-effects of default constructor generation. however, on recompilation of classes, compiler may taking shortcut of using available class bytecode previous compilations. bytecode reuse might explain how subsequent recompilations correctly identify unreachable constructor. compiler cannot determine via code analysis can determine via bytecode analysis (hypothetically of course, can't sure without access exact compiler/machine/etc).
why execution of provided class construction generate illegalaccesserror non-deterministically despite no changes compiled code?
if @ outer
class, constructor contains no branches or method calls prevent it's removal optimization. generated default constructor of extendedouter
may subject optimistic removal jit compiler. reply question require determining jit compiler determine method , constructor calls.
the jit compiler complicated topic, isn't unlikely empty constructor phone call inlined or replaced no-op transparently jit compiler. if jit compiler making code replacements , optimizations, lead inconsistent code paths across multiple executions of program. jvm , it's jit compiler not guaranteed produce deterministic output.
java inheritance constructor
Comments
Post a Comment