This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I had a weird problem today. I was experimenting with a framework library and came across a compile-timer error that deals with inheritance and constructors. I wrote up the following code to demonstrate the problem:
BTW, yes, each is in a separate file. The javac compiler reported the following:
If I go and uncomment the empty constructor in DBObject.java it gets rid of the first error. If I add an empty constructor to DBQuery.java, it does nothing. If I add the parameter "Hashtable h" to the empty constructor (and do nothing with it) then it compiles just fine. My question is why do I need to add the constructors? Shouldn't DBQuery be allowed to call the public constructor extended from DBObject? Do all constructors for an abstract class need to be implemented in a subclass? I want to enforce a rule that all objects extending DBObject must use the constructor with the Hashtable so that I know each one has a proper database connection at object creation time so there's no lag when calling getConnection(). If I have a default constructor that doesn't do setup a connection, then the object is in an unknown state and not very useable. Is there a way to do that? Or am I crazy to be thinking I can do this?
CJP (Certifiable Java Programmer), AMSE (Anti-Microsoft Software Engineer)
Author of Posts in the Saloon
Constructors are not inherited. You do not have a constructor in the DBQuery class that takes a Hashtable.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Joined: Aug 02, 2001
Thanks, Cindy. While not quite what I was looking for in terms of explanation, it triggered something in my flu-delerious brain (I hate coding when I'm sick - I get brilliant flashes more often, but the basic stuff ends up worse than normal). I left the empty constructor in DBOject commented out, but added a constructor to DBQuery accepting a Hashtable, and in it called super( hash ), which seems to have done the trick. Seems kinda silly that you have to do this, though. Why wouldn't you want to inherit constructors?
Joined: Sep 29, 2000
Well for one thing - the rule is that the name has to match. If you inherit the parent classes constructor, it will not have the same name as the subclass. If you allowed constructors to be inherited then they could be overridden. If that happened then you could not guarantee that the thing constructed would actually be a correctly formed object of the parent class. What you did is exactly what the creators intended should be done.
Joined: Aug 02, 2001
Ok, that makes sense. I should probably go back and re-read a basic java book. Last time I did that was in a class that used Java 1.02 about 5 or so years ago! I've been using Java on and off since then, but only in the past year has it expanded enough to be truly useful for the type of work I do. Thanks again!