inheritance - java: enforce no arg constructor in subclass at run time -
i know there's no way enforce @ compile time, hoping there'd way @ runtime maybe using reflection.
you obvious way, reflection:
public class superclass { public superclass() { try { // constructor no arguments this.getclass().getconstructor(); } catch(reflectiveoperationexception e) { throw new runtimeexception("subclass doesn't have no-argument constructor, or constructor not accessible", e); } } }
(note getconstructor
return constructor if public; allow private constructors too, use getdeclaredconstructor
).
Comments
Post a Comment