Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator overloading and overriding in Java

Tags:

java

What is the difference between operator overloading and operator overriding?

Are they the same in inheritance and console program?

like image 762
rajesh Avatar asked Aug 30 '10 07:08

rajesh


2 Answers

Operator overloading and overriding are not supported in Java.

Check following desc quoted from : http://java.sun.com/docs/white/langenv/Simple.doc2.html

2.2.7 No More Operator Overloading

There are no means provided by which programmers can overload the standard arithmetic operators. Once again, the effects of operator overloading can be just as easily achieved by declaring a class, appropriate instance variables, and appropriate methods to manipulate those variables. Eliminating operator overloading leads to great simplification of code.

like image 91
YoK Avatar answered Sep 30 '22 13:09

YoK


You cannot override (nor overload) operators in Java.

In some other languages you can, and difference between operator overloading and overriding is the same like between function overloading and overriting. E.g. in Scala operators are just functions.

like image 22
amorfis Avatar answered Sep 30 '22 14:09

amorfis