Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-22-2008, 02:53 PM
Member
 
Join Date: Jul 2008
Posts: 1
Andrefs is on a distinguished road
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 03:18 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-22-2008, 06:27 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Norm is on a distinguished road
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.
Or ...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] If a object equals another object, do they contain the same data? bobleny New To Java 1 04-18-2008 12:10 AM
which class is superclass and subclass? java_fun2007 New To Java 0 12-11-2007 10:55 PM
SuperClass of an Object Java Tip Java Tips 0 12-06-2007 04:51 PM
Creating Document object for XML parsing Java Tip Java Tips 0 11-19-2007 06:12 PM
Operator < cannot be applied to java.lang.Object, Object Albert Advanced Java 1 07-13-2007 05:19 PM


All times are GMT +3. The time now is 08:51 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org