Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

OOP help: Accessing subclass object data when passed as superclass

807598May 4 2006 — edited May 4 2006
I don't believe I'm thinking through this problem correctly, and would appreciate any advice. Also, I'm sure I'm not using my terms correctly, but as the forum is for the new, I hope someone will correct me on that front as well.

I am building a application that will generate a set of summary data tables from a database.

I have a super class, Motor, which provides a set of core data processing methods. I have a series of subclasses: Motor1, Motor2, ...MotorN, which extend Motor to perform specific data processing methods. Each motor subclass generates a similarly structured table of results. I have an additional Display class, with a method for displaying the data structure processed by a Motor object. I have a set of JSP pages that runs the subclass motors then display the results. None of it actually works yet, mind you, but I hope you get the idea.

At the moment I am stuck on the following: depending on which Motor subclass was used to create the Motor, some differences exist in information regarding that result, which I want to incorporate conditionally into Display.

For example, method Motor1 creates a Motor1 object accepting a row filter parameter. Motor2 creates a Motor2 object accepting a column function parameter. These are strings. Since each subclass object Motor1, Motor2, etc is an extension of Motor, I pass them as Motor objects to Display.

However, I want the strings associated with the subclass specific parameters to be available to the Display method. So if a row filter was used, I want the Display to note the row filter, etc. But since the subclass motors are passed as super class motor, the subclass parameter objects like row filter or column function don't accompany them. How do I get these into the Display?

I think I have two or maybe three options:
1. Create a set of methods in Display that cast each subclass object as the super class, then call the main Display method passing the cast motor object along with making the subclass strings available.

2. Modify the Motor class to accept the additional parameters, even though they are only used by the subclasses. In this way they would be available when the subclass objects are passes as super class object.

3. Create a method for each subclass motor that pass the values in question, and call these methods in Display. I think this would be tricky or maybe impossible to get to work correctly, as at any time there could be multiple motors, so I'm not sure how the Display method would be able to access the subclass method to get information about a specific object instance, so if you think this is the best approach, please also provide clues as to how.

All of these approachs seem ugly to me for one reason or another. Is there a simpler way to do this or think about this? If not, are any of the above approaches more or less acceptable?

Thanks,
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 1 2006
Added on May 4 2006
7 comments
733 views