3

Possible Duplicate:
operator overloading and overriding in java

I want to know if it is possible to implement and overwrite the + and ++ operators.

Example:

public class MyClass{
 private myIncrement =0; //with its getter and setter
}

.... (another class code)
MyClass myClass = new MyClass();
myClass++;
myClass.getIncrement(); //this will return 1.

And also, if I want to create an Integer class that gives me only multiple of three in a way that when I use i++ on a MultipleOfThree instance it sums three to the current instance instead of sum 1.

Community
  • 1
  • 1
Christian Vielma
  • 15,263
  • 12
  • 53
  • 60
  • In a word, drug out to over 15 characters: No. – Hot Licks Jul 16 '12 at 17:09
  • No you can't. See this (long) disussion if you have some spare time ;-) http://stackoverflow.com/questions/77718/why-doesnt-java-offer-operator-overloading – assylias Jul 16 '12 at 17:09
  • While you can't do this specifically with the `++` operator, you can of course abstract the concept into an `increment()` method. Similarly, you can create a new class that simulates the "multiple of three integer", though again you couldn't use the `++` operator. This is not a bad thing: it's very confusing to use the `++` operator and end up 3 integers ahead. Forcing you to create a method name like `incrementByThree()` makes things a lot clearer. – dlev Jul 16 '12 at 17:10
  • 1
    I'm curious as to why this was down-voted, can anyone shed some light on that? It seems like a reasonable question from someone new to java. – zmf Jul 16 '12 at 17:11
  • No. From James Gosling: "I left out operator overloading as a fairly personal choice because I had seen too many people abuse it in C++." http://www.gotw.ca/publications/c_family_interview.htm – Liggy Jul 16 '12 at 17:14
  • 1
    @zmf I didn't downvote, but probably because lack of effort; this is searchable. – Dave Newton Jul 16 '12 at 17:14
  • by asking this question, you caused more than 250 reputation to be gained – Sam I am says Reinstate Monica Jul 16 '12 at 17:15
  • And then to think that operators _are_ overloaded at the compiler level. For instance int+int calls a different function than float+float. Oh well. – Mr Lister Jul 16 '12 at 17:16
  • 2
    @DaveNewton Seeing the op use the term 'overwrite' to describe overloading is illustrative... I.E. they cannot search for the right term if they don't know the right term. This seems to be use of SO exactly in the spirit of its design, though I concede to your suggestion that they could have dug this answer out of a google search bar. – zmf Jul 16 '12 at 17:36
  • @zmf Google works for me, including things like "implement java plus operator" etc. Do maybe out was downvoted because the OP is bad at google--you're debating the wrong person, I didn't downvote. – Dave Newton Jul 16 '12 at 18:55
  • @DaveNewton Ha, wasn't debating you. Sorry that it sounded that way. I was genuinely curious as to what people were thinking, and you offered an opinion on that; Thanks! – zmf Jul 16 '12 at 19:07

8 Answers8

8

You can't do this: Java doesn't support operator overloading. Handle your examples with additional instance methods, which is usually clearer anyway.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • Often operator overloading is clearer, especially when dealing with mathematical constructs (such as a class for a vector / matrix). I would argue that just because additional instance methods are "usually" clearer is not sufficient justification for leaving a useful language feature like this out of the language. – matts1 Aug 22 '15 at 07:36
6

NO. operator overloading NOT possible in java. Here is an interesting discussion about why.

kosa
  • 65,990
  • 13
  • 130
  • 167
5

No. Java doesn't have operator overloading.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
5

No, it is not possible in Java.

dbf
  • 6,399
  • 2
  • 38
  • 65
4

You can't do this. It is not possible in java.

Uchenna Nwanyanwu
  • 3,174
  • 3
  • 35
  • 59
2

Not possible in java. It should be, but it isn't

1

Operator overloading is impossible in Java.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
1

Java has one and only one Overloaded Operator + , which canNOT be custom overloaded.

Mostly one can see its use in Conversion of Numeric type to String.

int x = 5;

String s = x + "" ;
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • given all the answers, I notice that any operator can be overloaded. Can you name one that can? – Christian Vielma Jul 16 '12 at 17:22
  • See my example given in the answer... For further details please see the Head First Java by Kathy Sierra and Berth Bates. – Kumar Vivek Mitra Jul 16 '12 at 17:25
  • I am not mumbling some theoretical mumbo-jumbo what one said and all followed, i have given an example..you can try it out.. – Kumar Vivek Mitra Jul 16 '12 at 17:26
  • Even if you dont give me an upvote, please reply back after trying it out... Proving a point for me is more important than the vote. Please do that... – Kumar Vivek Mitra Jul 16 '12 at 17:28
  • 2
    This is not considered overloading, it's called type coercion. The + operator has special cases in the compiler and interpreter to handle this. From the java whitepaper: 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. – Matt Jul 16 '12 at 17:37
  • @Matt i think you need to read my Bold lines again, I said "CANNOT BE CUSTOM OVERLOADED" that means in proper english, that a programmer cannot overload it.... – Kumar Vivek Mitra Jul 17 '12 at 02:55
  • overloading is now what the + operator is. As i said there's specific code in the compiler to handle it and replace it with stringbuilder calls. Overloading implies that it's a language feature that is available (even if only on a limited basis) to certain classes. But i suppose James gosling was lying when he said the language does not have overloading nor is it needed. I mean after all, what does he know. – Matt Jul 17 '12 at 11:33