0

I've created a Vector class and want to add be able to add the Vector's X and Y by just doing

Vector a + Vector b

Is it possible to do this in Java?

Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420

1 Answers1

1

Sorry, but you cannot define or overload operators in Java. Implement an add method. You can have the add method return this, to allow chaining of operations (e.g., sum = a.add(b).add(c)), but that does not always result in the most readable code.

Ted Hopp
  • 227,407
  • 48
  • 383
  • 507