1

I am using JComboBox with a custom class object, and the equals method is over-ridden, and integrated very deeply into the code.

The problem is that if two objects are equal in a JComboBox drop down, then if one is selected all are selected, and the get selected index returns -1.

Is there a ways to cast a Vector<ObjectA> to a Vector<ObjectB>? I tried

Vector<Clas_2> v_temp=(ca.courses.get(i).classes);

and

Vector<Clas_3> v_temp=(ca.courses.get(i).classes);

Where Clas_2 is a parent of Clas_1 and Clas_3 is a extends of Clas_1, but neither of them compile.

All i need is JComboBox not to use the over-ridden equals method.

*Note I know I can cast each individual element into a new array, but would rather have a more memory efficient solution.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Steven Feldman
  • 833
  • 1
  • 15
  • 28

2 Answers2

1

No, not without being type-unsafe. But you can cast Vector<Clas_1> to Vector<? extends Clas_2> though which should solve your problem.

Since Clas_2 is a parent class of Clas_1, anything you get from a Vector<Clas_1> is an instance of Clas_2, but you cannot add any Clas_2 to a Vector<Clas_1> since not all instances of of Clas_2 are instances of Clas_1. The extends syntax makes that distinction.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • Hmm, I tried this and it compiles, but it still seems to use the equals method in Clas_1, instead of Clas_2 (which always returns false) – Steven Feldman Mar 15 '11 at 00:58
  • in fact, it doesn't solve his **real** problem. No amount of class casting will allow him to override the `equals` behaviour. You just can't do it that way in Java. – Stephen C Mar 15 '11 at 01:27
1

Changing the type you declare a variable as in code won't change what equals() method is called. It will always be the overriden one, irrespective of what you cast it to. This is how polymorphism works. You'll need to create a different class if you want a different implementation of equals.

Melv
  • 2,201
  • 16
  • 14
  • If you change it to type Clas_2 or Clas_3 who has an eqauls method that always returns false, then would it not use the equals method of the current type? – Steven Feldman Mar 15 '11 at 00:59
  • No. It will always use the equals method of the underlying type. Casting a variable doesn't actually change it's underlying type, just what it appears as to your code. The type is a way of asserting what the type must "at least" be. What it actually is may be completely different. I'd recommend doing some reading on `polymorphism` and how it fits into Java and OO programming. – Melv Mar 15 '11 at 01:04
  • I guess I have been coding in C++/C too long, where u can switch types really easy. is there a way to make JComboBox not use the equals method? – Steven Feldman Mar 15 '11 at 01:08
  • 1
    That's not possible, no. What you'll need to do is change the actual underlying type. Writing a new class, which you hand a reference to a `Clas_1` or `Clas_2` etc. but provides the equals implementation you want might be an effective way to do this. I'm guessing if you can't implement an accurate equals method in the class you have, then they may not be the correct types to be putting into your JComboBox in the first place. Maybe post your equals implementations in each of your classes? – Melv Mar 15 '11 at 01:19
  • I think the best bet would be to have a for loop going through my Vector, and adding each Clas_1 using the toString(). Thanks for your help. – Steven Feldman Mar 15 '11 at 01:31
  • A string representation is a quick solution to this, which may be fine depending on your application. You'll be the best judge of this. Glad I could help. – Melv Mar 15 '11 at 01:35