File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Method to build subclass objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Method to build subclass objects" Watch "Method to build subclass objects" New topic
Author

Method to build subclass objects

Alex Birmingham
Ranch Hand

Joined: May 22, 2008
Posts: 54
Okay.... I have a text-file that holds the information to create a series of 'rooms.' Type, X, Y, name, description, exits. Here is part of the text-file:


townRoom:0:0:Townsquare:Lots of birds and stuff!:north/south/east
wildernessRoom:-20:-10:A Ravenous Forest:Geez things are not looking good for you!:north/south


I want to create a builder that instantiates Room objects based on that information, but not just Room objects, townRoom objects, or wildernessRoom objects, or whatever subclass the specific room is. So I did this:



Which works fine, except for the fact that because the actual instantiation occurs within if(), the compiler can't actually find newRoom when I start in with my setters. In other words, it doesn't trust that newRoom actually exists. So then I thought I might create the object above my if statement, and then use the if statement to correctly cast it, except that apparently I can't cast a superclass as a subclass. I've been wracking my brain trying to come up with another angle to go about this, but I'm out of ideas =/

Furthermore I'm concerned about the fact that this seems to indicate that I have to create an 'object-builder' method for every single one of my subclasses? What about when I'm creating monster objects, and there are 200 different types of monsters?
[ June 05, 2008: Message edited by: Alex Birmingham ]

- SCJA 1.0
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

by the time you get to your setters, the object DOESN'T exist. The reference has gone out of scope, so the object is lost and available for garbage collection.

you need to do something like this:



you can make your reference the superclass type, and declare it before you start your if block. note that you don't NEED to cast it. This is what polymorphism is all about. you create teh wildernessRoom, which IS a room, so the reference is valid. when you then call the setters or whatever, the subclasses version will run automatically (if they have indeed been overridden).
[ June 05, 2008: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Alex Birmingham
Ranch Hand

Joined: May 22, 2008
Posts: 54
Thanks!
[ June 05, 2008: Message edited by: Alex Birmingham ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
As an aside, the general convention is to have class names start with an uppercase letter. Not following that convention will make your code considerably harder to read for others.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method to build subclass objects
 
Similar Threads
how to extend a static builder
Program doesn't complete, but no errors?
Beginner Trying
Please Help Me
is it an array i need?