Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redefine/overload type casting

Tags:

java

casting

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).

like image 389
Faraz Masroor Avatar asked Oct 19 '25 09:10

Faraz Masroor


1 Answers

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

like image 58
peter.petrov Avatar answered Oct 21 '25 22:10

peter.petrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!