0

Is there a way to override the assignment operator in Java ?

For example, I can always do something like:

AJSDate startDate = new AJSDate("20090811");

but I would rather have it as:

AJSDate startDate = "20090811";
kCC
  • 577
  • 1
  • 5
  • 7
  • no you can not let you to do that. use set method rather than thinking about operator overloading – Esterlinkof Aug 15 '15 at 00:02
  • overloading, not overriding - overriding means replacing the functionality of a parent class (or in other languages besides Java, a parent namespace). Operator overloading means writing an additional functionality for the operator, depending on its context. – La-comadreja Aug 15 '15 at 00:12

2 Answers2

6

No. Java doesn't support the overloading of operators.

You can't even extend String to do something like this:

class AJSDate extends String {
}

public class HelloWorld {
     public static void main(String []args){
        AJSDate ajs = "This is a test";
        System.out.println(ajs);
     }
}

You would get the following compile-time errors:

error: cannot inherit from final String 

(for trying to extend String - String is a final class which means it can't be extended)

error: incompatible types 

(for AJSDate ajs = "This is a test"; - the operands on either side of the = operator aren't type-compatible.)

If you really want to write a program that allows operator overloading, use another language such as C++, Scala or Ruby.

A more minor point, but if you would like to create an object with the functionality of another object (i.e., that extends another object) then stylistically, you probably want to end the name of its class with the name of its parent class. E.g. if it extends class Foo, call it AJSDateFoo.

La-comadreja
  • 5,627
  • 11
  • 36
  • 64
  • 2
    I think that's a bad example. Even if you could extend `String`, that wouldn't work. It's like trying to do `Child childRef = new Parent();`. – Sotirios Delimanolis Aug 15 '15 at 00:14
  • The point of the example is to show some other reasons for the lack of a workaround, more than anything else. Strings are unique in Java, in that they act in many ways as both primitives and objects. If the Java maintainers decided to allow extensions of String, Java would need to develop a policy for whether and how the primitive-like assignment would work in child classes of String. At least in theory, I don't see why this is impossible to implement. – La-comadreja Aug 15 '15 at 00:18
  • After all, Java was extended to allow autoboxing of primitive type wrapper classes which presented a not-so-different problem. – La-comadreja Aug 15 '15 at 00:34
  • FYI - here's a link explaining that String and wrapper classes are immutable and final to maintain call by value for primitives. But call by value is not required and many languages successfully use call by reference. http://www.explain-java.com/is-string-mutable-in-java-why-wrapper-classes-immutable-in-java/ – La-comadreja Aug 15 '15 at 00:37
  • *"The point of the example is to show some other reasons for the lack of a workaround ..."* - Well its a `#Fail`. What it is actually showing that a *specific* workaround doesn't work. But that specific workaround has a whole lot of negatives that would most likely rule it out ... even if it "worked". For example, most of String's methods would make no sense on a "date" object. – Stephen C Aug 15 '15 at 01:21
  • The real reason is that Java doesn't have user-defined operator overloading is that it was ruled out explicitly by the Java designers as making the language too difficult to understand. They saw what it did in C++ and said "No Way!". Even if you could persuade them it would be good, adding operator overloading after the fact would most likely be technically impossible without breaking backwards compatibility. – Stephen C Aug 15 '15 at 01:23
  • @StephenC Matz, the creator of Ruby, says Operator Overloading? Yes Way. Ruby's flexibility is one of the reasons that I program in Ruby most of the time :) – La-comadreja Aug 15 '15 at 01:26
  • Fine. Different language. Different goals. I've programmed in Ruby myself, and operator overloading was one of the things that caused me difficulties. But the point real is that the Java designers made a conscious decision to rule it out. And there is ZERO chance they will change that. – Stephen C Aug 15 '15 at 01:28
  • well, the non-extensible operators are part of Java's culture just like the everything-is-an-extensible-object-including-operators is part of Ruby's. I think we basically agree here. I hope you and @kCC can enjoy the pastime of opening up Ruby's class Object and watching whatever you've done to the operators appearing in every single class. Time to close this before we go off-topic. – La-comadreja Aug 15 '15 at 03:10
0

Not in Java itself. Unfortunately for those cases where operator overloading is useful.

However, as operator overloading is basically syntactic sugar the JVM doesn't forbid it. Other JVM languages e.g. Scala has some operator overloading support.

drrob
  • 632
  • 7
  • 16