Results 1 to 2 of 2
- 07-22-2008, 01:53 PM #1Member
- Join Date
- Jul 2008
- Rep Power
Parsing a superclass object to subclass object dynamicly
I have about 20 different classes that extend a superclass. Each subclass has some methods that are not implemented in the superclass.
At runtime I need to parse the superclass object to whatever subclass object for my program to work correctly. How do I do this?
Manually I would do something like
Superclass super = method.getSomething();
Subclass sub = (Subclass)super;
This works fine, but I don't know how to do this at runtime because don't know which class that it is to be parsed. Any ideas?
thanks in advance
EDIT: I mean casting obviously.
Last edited by Andrefs; 07-22-2008 at 02:18 PM.
- 07-22-2008, 05:27 PM #2
You can use instanceof to check the type of an object and then cast it from there.
I don't think you have the best design for your app. I'm not good at OOP.
The different logic required for each subclass should probably be in the subclass.
Or you should use an interface so that all the subclasses have the same type.
- By Albert in forum Advanced JavaReplies: 2Last Post: 11-26-2010, 03:12 AM
- By bobleny in forum New To JavaReplies: 1Last Post: 04-17-2008, 11:10 PM
- By java_fun2007 in forum New To JavaReplies: 0Last Post: 12-11-2007, 09:55 PM
- By Java Tip in forum Java TipReplies: 0Last Post: 12-06-2007, 03:51 PM
- By Java Tip in forum Java TipReplies: 0Last Post: 11-19-2007, 05:12 PM