Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dealing with uninstantiated abstract classes 1

Status
Not open for further replies.

carpeliam

Programmer
Mar 17, 2000
990
US
this kind of code happens to me all the time:<br><br><FONT FACE=monospace>MyAbstractClass objectName;<br>try {<br>&nbsp;&nbsp;&nbsp;&nbsp;objectName = somethingThatNeedsToBeInTryBlock;<br>} catch (Throwable t) {<br>&nbsp;&nbsp;&nbsp;&nbsp;System.err.println(&quot;Error: &lt;&quot; + t.getMessage() + &quot;&gt;&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;t.printStackTrace(Trace.toPrintStream());<br>}<br>Object o = objectName.returnObject();</font><br><br>this is about as generic as I can get. This obviously won't work because <FONT FACE=monospace>objectName</font> may not have been initialized, and you can't instantiate it because it's an object of an abstract class. So... what's the workaround for this? I know and you know that this object has been initialized, but seeing as how we're outside of the try block, Java doesn't know it. How can we either (a) instantiate the object (and I'm not creating a temporary object of type <FONT FACE=monospace>MyAbstractClass</font> just for the purposes of avoiding the error message if I can help it) or (b) get Java to know that it's already been instantiated? Like, this line of code (the one that deals with the already-initialized object) CAN NOT (beyond Java's shadow of a doubt) be executed unless the try() block is successful? And assume that I can't just throw all everything below the try block inside of the try block- that's just bad. Especially since I'm doing recursion, and I'm NOT calling recursion from inside of a try block. Eh Hmm. *cough cough, hack hack*<br><br>So... can anybody help me out? I've run into this problem so many times (just about every time I deal with try blocks and abstract classes in the same scope), I'm willing to give a vote (2 if I can help it! I'll figure out a way, I promise!) for TipMaster of the Week to whoever comes up with the best answer. (Gah, I'm running a contest on somebody else's website, I should be shot. I hope the admins don't kick me off. NOTE: I am not running a contest.) And by &quot;best answer&quot;, I mean, best answer- I guess I could ship this code off to those bug fixer guys, but I guess I already knew that. Neat idea, but I think company-confidential code or anything that resembles it has to stay out of other companies' hands (hence the word confidential, I guess). So.. can somebody PLEASE give me a hand? This problem (or at least this type of problem) has been bugging me for months, and it's really a pain to work around it for specific examples. Thank you so much (so much you wouldn't believe it!) to whoever can help me.<br><br>Thanks again (and again and again and again after you help me). NOTE: If all I can do is create a temporary object of the abstract class and you're the guy who tells me something I really didn't want to know, you're getting nada for TipMaster of the Week votes. Kill the messenger. But if it really is all I can do (and you're sure about this), please tell me, don't hold back. Thanks (yet) again.<br> <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
 
wait- would this work?<br><br><FONT FACE=monospace><font color=red>Object tempObjectName = new Object();<br><br>//you need this to hold the type of the object;<br>//seeing as how it could be a subclass of MyAbstractClass,<br>//you can't cast it as an object of MyAbstractClass<br>Class c = Object.class;<br>try {<br>&nbsp;&nbsp;&nbsp;&nbsp;c = itemToBeThrown.getClass();<br>&nbsp;&nbsp;&nbsp;&nbsp;tempObjectName = (Object)itemToBeThrown;<br>} catch (Throwable t) {<br>&nbsp;&nbsp;&nbsp;&nbsp;System.err.println(&quot;Error: &lt;&quot; + t.getMessage() + &quot;&gt;&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;t.printStackTrace(Trace.toPrintStream());<br>}<br>MyAbstractClass objectName = (c)tempObjectName;<br>Object o = objectName.returnObject();</font></font><br><br>If that works, then I'm not giving out any tipmaster votes, sorry. In fact, because I'm so proud of myself for figuring this out, I think that (at least!) two of you should give me TipMaster of the Week votes for getting this. (Gah, I'm polling for votes, I should be shot. I hope the admins don't kick me off. NOTE: I am not polling for votes. OK, so maybe a small part of me is just for kicks, but it's not like it matters to me.) And not only that, but I'll write my own Java (Sun) FAQ and point myself to it. Hehehe. Yeah, go me. Ok, thanks for the help. And if an admin sees this, can you guys please implement some colors other than red? It just looks nasty on that background, sorry. I tried [brown]some brown text[/brown], but that didn't work. Obviously. Thanks again... <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
 
Dear Liam,<br><br>This might not be at all what you are asking... I had trouble following your posts.<br><br>1) You can not create an instance of an abstract class... that's why it's 'abstract'.<br><br>2) You can have an instance of an abstract type that is actually of a derived concrete type... like this:<br><br>abstract public class base{<br>&nbsp;&nbsp;abstract public String foo(){ return &quot;base.foo&quot;; }<br>}<br><br>class mybase extends base{<br>&nbsp;&nbsp;public String foo(){ return &quot;mybase.foo&quot;; }<br>}<br><br>class baseFactory<br>{<br>&nbsp;&nbsp;static public base getObject(){ return (base)new mybase(); }<br>}<br><br>static void main( String args[]){<br><br>&nbsp;&nbsp;base x = baseFactory.getObject();<br>&nbsp;&nbsp;System.out.println( x.foo()); // prints: mybase.foo<br>}<br><br>This is polymorphism.<br><br>NOTE: This is example code, not compiled, probably needs to be split up into files, errors corrected, etc. I don't like to do this but I don't have a compiler available at this moment. I hope I don't regret posting it.<br><br>Hope this helps<br>-pete
 
hi imotic!<br><br><br>Your problem is quite simple....;<br><br>Initialize the abstract class object to null and everything will work. Like this:--<br><br><br><br>MyAbstractClass objectName=null;<br>try<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;objectName = somethingThatNeedsToBeInTryBlock;<br>} <br>catch (Throwable t) <br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;System.err.println(&quot;Error: &lt;&quot; + t.getMessage() + &quot;&gt;&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;t.printStackTrace(Trace.toPrintStream());<br>}<br>Object o = objectName.returnObject();<br><br><br>Regards<br><br><br><br><br><br>
 
Palbano-<br>I know.. let me re-explain with a real world example so it will make more sense. Now this probably doesn't happen all that often, but I have a hashtable filled with what should be collections; they could be vectors, they could be anything as long as it's a subclass of Collection. (Collection will be our abstract class.) But here's our problem- what if you're not positive that everything in the hash table is a collection? Here's the example code:<br><br><FONT FACE=monospace>for (Enumeration e = myHash.keys(); e.hasMoreElements();) {<br>&nbsp;&nbsp;&nbsp;&nbsp;String key = e.nextElement().toString();<br>&nbsp;&nbsp;&nbsp;&nbsp;Collection currentCollection;<br>&nbsp;&nbsp;&nbsp;&nbsp;try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;currentCollection = (Collection)myHash.remove(key);<br>&nbsp;&nbsp;&nbsp;&nbsp;} catch (Throwable t) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println(&quot;Error: &lt;&quot; + t.getMessage() + &quot;&gt;&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.printStackTrace(Trace.toPrintStream());<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;for (Iterator iterator = currentCollection.iterator(); iterator.hasNext();) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object currentObject = iterator.next();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// do something<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>}</font><br><br>You'll get an error from the for (Iterator iterator = currentCollection.iterator(); iterator.hasNext();) loop because currentCollection has not been initialized. That's what I meant. But thanks :eek:)<br><br>And as for juliaroberts... you get a vote! wow, I missed the glaringly obvious. You can assign without instantiating. That's wonderful. Woohoo! And the code goes on... As for the second vote... if you'd like, I could ask some insanely easy question (apparently like the one above), you could answer it (or just post to it, I guess), and I could nominate you once again. (I hope that's legal!) It's up to you.. you can email me, my email address is public. Thanks again, you've saved me from stupidity. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top