• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there anyway to force a subclass to implement a default constructor  RSS feed

 
Derick Potgieter
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anyway to force a subclass to implement a default constructor.

Be it reflection, anotation??

But the trick comes in, i have to get a compile time exception.

Rgds
Derick
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16045
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there is a way to force a subclass to have a default (i.e., no-args) constructor, which is checked at compile time. With reflection you could find out if a class has a no-args constructor, but not at compile time. I don't know a lot about annotations (I haven't used them in real world projects yet), so I can't tell you if it can be done with annotations.

Why would you want this?
 
Derick Potgieter
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are using a custom transaction model, and the model requires that the subclass implementing the SQL has a default constructor. Not to sure why, i just know that if it isn`t there some audit infomation doesn`t get generated.

Any one else with any ideas???
 
Paul Clapham
Sheriff
Posts: 22697
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To me that sounds like a problem with the auditing. If it should report an error instead of ignoring it when a class doesn't have the right constructor, it should be changed to report the error.
 
Michael Duffy
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Derick Potgieter:
We are using a custom transaction model, and the model requires that the subclass implementing the SQL has a default constructor. Not to sure why, i just know that if it isn`t there some audit infomation doesn`t get generated.

Any one else with any ideas???


"custom transaction model" - red flag. what's not done in JTA that is required by your problem?

sounds like a custom persistence model. why is this necessary when there are so many ways to do it already available to you? what are you doing that improves on jdo, o/r mapping, ejb cmp? there's hibernate, ibatis, toplink, cayenne, kodo, etc.

unless you've got a good reason for doing this, it sounds to me like you're doing a lot of work for little benefit.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too think you're looking up a dead end here. Standing the design of the language on its head to realize an obscure feature benefit is the first solution you should cross off the list of ideas.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's two ways to enforce something like this: at compile time, and at runtime. Come up with a runtime test suite that a component has to pass to be used with the framework. One of the tests could try to use Class.newInstance() to create an instance, or fail.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try this:
http://jtiger.org/javadoc/org/jtiger/assertion/Reflection.html


As you have discovered, concrete behaviour inheritance has no legitimate existence when we observe time to move. If you like, it is best to explain the full problem so that a "more correct" alternative can be offered. The problem should consist of an accurate detail of all abstractions.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!