1

After searching SO and the web, I've yet to come up with an answer for this in Java. It's pretty simple.

I've got abstract class ParentClass. Class X and class Y both inherit from ParentClass. I've also got class DataInputDialog extends JDialog, which is meant to provide a user interface for entering data into class X and class Y instances. DataInputDialog class looks like below:

public class DataInputDialog extends JDialog {

    public DataInputDialog(ParentClass xOrY) {
        //calls super constructor, makes basic components such as
        //  buttons and panels that are used in either X or Y data entry

    }
}

now I'd like to have a switch statement that would determine what type of ParentClass was passed to DataInputDialog. However, I've got no clue how to accomplish that. I've tried:

switch (xOrY.getClass().getTypeName()) {
    case X.class.getTypeName();
}

and a couple other variations, but it always comes up with an error on the case statement saying either: it has to be a constant String, or that it cannot be converted to an integer.

How do I get this switch statement to determine if xOrY is an instance of X or Y?

izrik
  • 918
  • 2
  • 9
  • 20
corbfon
  • 135
  • 1
  • 13

2 Answers2

8

For a switch to work the value must be a primitive value, a String or an Enum value. In OO code switch statements are something of a code smell; generally indicating you have missed a chance to use polymorphism.

In functional languages you might be able to use a case statement to do what your are trying.

Java is OO so I would make use of polymorphism, you will have a cleaner design.

Polymorphism Is an OO concept that basically says that two objects/classes can be considered to "BE" the same if one is the super set of the other. That is, if the objects/classes satisfy the IS-A relationship. For example if you have a Car class, FastCar and VintageCar classes that extend Car both FastCar and VintageCar are Car classes and thus satisfy the IS-A relationship, and thus they can be used any where in code where Car can be called. The implication here is that if Car...accelerate exists then FastCar.accelerate exists and can have different characteristics from VintageCar.accelerate so when call accelerate the code doesn't need to know the subtype but will call the correct accelerate method.

Gavin
  • 1,725
  • 21
  • 34
  • Came here to say this. For the benefit of the newbie OP, I think you should elaborate on how to make use of polymorphism. – Kevin Krumwiede May 07 '16 at 19:21
  • @KevinKrumwiede a very good point, not sure I give the best explanation, but lets give it a go. – Gavin May 07 '16 at 19:24
  • @Gavin So, in this circumstance I've got some code that will be the same in any case, and some code that is unique. Would you suggest I create a method in the super class to accomplish the part that is similar, and then package only the unique parts within the subclasses? – corbfon May 08 '16 at 07:01
  • @corbfon Yes I think so. Code that is the same should go in the super class, code that is unique to the sub class in the sub classes. methods that are the same but that can have unique implementations should be abstract in the super class and should be made concrete in the sub classes – Gavin May 08 '16 at 10:19
0

A Switch case in java needs a compile time constant. In your case the class name is not constant at compile-time. It is only determined at run-time. I think you would have to write the old-fashioned 'if' case for this.

    if(xOrY.getClass.getTypeName().equals(X.getClass.getTypeName())

Generally speaking, getting the implementation details is generally a bad idea. You can actually add the implementation details in the instance variables if you need it so bad.

sriram manoj
  • 155
  • 1
  • 9