Which class am I?
In my program I have quite a few (similar) classes each which extend JPanel. Each of these classes has implemented an interface method called isComplete() which validates user entered information on that particular panel. Now, I want to invoke the appropriate isComplete() method depending on which panel the user has worked on.
My call would look like this:
if (classExtendingJPanel.isComplete()) then....
I have never figured out how to get the correct classExtendingJPanel in this case. Is there a way to do this with ClassLoader or how? Any suggestions are greatly appreciated.
[615 byte] By [
FeliceM] at [2007-9-26 1:33:08]

Simply use polymorphism:if (isComplete()) will call the appropriate class's method.
Thanks so much you for your reply. Please let me explain a little more. The JPanel classes are passed to the class CustomTreeNode through CustomTreeNode's constructor. So if I just invoke isComplete() from within CustomTreeNode my program thinks that method should be found in the CustomTreeNode class. I need to somehow invoke the isComplete method for the correct JPanel class after it is passed into the CustomTreeNode class as a constructor parameter.
If you call it on a reference to the object, the appropriate class's method will be called.Perhaps you could post some code, and I could give you a more specific answer.
Below is the relevant code the way I have it in the program. I have used if statements to determine which of the JPanel classes I am getting as a userObject, just to make the thing run, but I realize that this is not good at all. I should be able to do the same thing "generically" by just calling isComplete on the userObject. (Of course I need to get userObject to getNodeStatus some way or another). I tried various scenarios but nothing works. The classes I have mentioned which are extensions of JPanel are the variable names that end in Card. Again, thanks so much for your help.
The constructor:
public CustomTreeNode(Object userObject) {
myCardName = userObject.toString();
getNodeStatus();
}
the rest
boolean getNodeStatus() throws NullPointerException {
if (myCardName.equals("Snow Drift")) {
myCardCompletionStatus = TreeController.snowDriftCard.isComplete();
} else if (myCardName.equals("Concentrated Loads")) {
myCardCompletionStatus = TreeController.concentratedLoadsCard.isComplete();
} else if (myCardName.equals("Collateral Loads")) {
myCardCompletionStatus = TreeController.collateralLoadsCard.isComplete();
} else if (myCardName.equals("Geometry")) {
myCardCompletionStatus = TreeController.geometryCard.isComplete();
} else if (myCardName.equals("Wall Cover")) {
myCardCompletionStatus = TreeController.wallCoverCard.isComplete();
}
}
Just call userObject.isComplete(), and ensure that the type of the userObject reference is the common superclass (not Object). Polymorphism will ensure that the proper class's method is completed.Polymorphism is the whole point behind interfaces and extending classes...
As schapel says, you call the isComplete method on the userObject, however the userObject must be cast to the appropriate interface.
Here's some code to illustrate:
// interface which all cards implement
public interface Completable
{
public boolean isComplete();
}
public CustomTreeNode(Object userObject)
{
// beware, userObject must implement Completable
// interface...
Completable myCard = (Completable)userObject;
myCardCompletionStatus = myCard.isComplete();
}
I understand how to do it now.Thanks a million,Felice
I understand how to do it now.Thanks a million,Felice
I am getting an error (which I commonly get) when I send messages (hence the repeated message.) which is not letting me send the Duke dollars. I'll forward them soon!
u can use public final Class getClass() method of the Object Class to get the runtime class of the object
But polymorphism should be used instead of run-time type identification such as instanceof or getClass().