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

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

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

asp.net mvc - breakpoint on javascript in CSHTML? -