Reputation: 238
Every object has a toString method inherited from the Object class it extends, which can be overloaded. IS there a way to do a similar thing with objects? Here is what I want to do: int a=n+1; where n is actually an object, but when used as an int has a specific value (eg. specified in the constructor).
Upvotes: 4
Views: 3692
Reputation: 39457
This is called operator overloading. You cannot do it in Java. And it is disallowed by design as far as I'm aware. I guess you're coming from C++ or C#, where this is possible.
http://en.wikipedia.org/wiki/Operator_overloading
Upvotes: 6
Reputation: 44439
No, you cannot use objects with arithmetic operators. In java these operators cannot be overloaded and adding an object to a number makes little sense.
Sidenote: overriding is not the same as overloading.
Upvotes: 0