Results 1 to 2 of 2
  1. #1
    Andrefs is offline Member
    Join Date
    Jul 2008
    Posts
    1
    Rep Power
    0

    Default 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 01:18 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,563
    Rep Power
    25

    Default

    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 ...

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2010, 02:12 AM
  2. Replies: 1
    Last Post: 04-17-2008, 10:10 PM
  3. which class is superclass and subclass?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-11-2007, 08:55 PM
  4. SuperClass of an Object
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 02:51 PM
  5. Creating Document object for XML parsing
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-19-2007, 04:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •