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

Popular posts from this blog

php - Passing multiple values in a url using checkbox -

compilation - PHP install fails on Ubuntu 14 (make: *** [sapi/cli/php] Error 1) PHP 5.6.20 -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -