Java Override Operator

In Java, the @Override annotation is used to override a method from a superclass. However, you can also override operators in Java by using certain methods and conventions. This allows you to define custom behavior for operators such as +, -, *, /, etc. This article will explain how to override operators in Java with code examples.

Operator Overloading in Java

Java does not support operator overloading by default, meaning you cannot change the behavior of operators for built-in types like integers or strings. However, you can achieve operator overloading in Java for custom classes by using method overloading and following certain conventions.

To override an operator in Java, you need to define a method with a specific signature that corresponds to the operator you want to override. For example, if you want to override the + operator for a custom class, you need to define a method with the signature public YourClass add(YourClass other).

Code Examples

Let's consider a simple class ComplexNumber that represents complex numbers and override the + operator for adding two complex numbers.

public class ComplexNumber {
    private double real;
    private double imaginary;

    public ComplexNumber(double real, double imaginary) {
        this.real = real;
        this.imaginary = imaginary;
    }

    public ComplexNumber add(ComplexNumber other) {
        double newReal = this.real + other.real;
        double newImaginary = this.imaginary + other.imaginary;
        return new ComplexNumber(newReal, newImaginary);
    }

    // Other methods for subtraction, multiplication, etc.

    @Override
    public String toString() {
        return real + " + " + imaginary + "i";
    }
}

In the ComplexNumber class above, we have defined an add method to override the + operator. Now, let's use this class to add two complex numbers:

ComplexNumber num1 = new ComplexNumber(2, 3);
ComplexNumber num2 = new ComplexNumber(1, 4);
ComplexNumber sum = num1.add(num2);
System.out.println("Sum: " + sum);

Class Diagram

We can represent the ComplexNumber class and its methods using a class diagram:

classDiagram
    ComplexNumber -- double real
    ComplexNumber -- double imaginary
    ComplexNumber : +ComplexNumber(real: double, imaginary: double)
    ComplexNumber : +add(other: ComplexNumber)

In the class diagram above, we have the ComplexNumber class with real and imaginary attributes, as well as the constructor and add method.

Sequence Diagram

Let's create a sequence diagram to visualize how the add method works when adding two complex numbers:

sequenceDiagram
    participant num1 as ComplexNumber
    participant num2 as ComplexNumber
    participant sum as ComplexNumber

    num1 ->> num1: real=2, imaginary=3
    num2 ->> num2: real=1, imaginary=4

    num1 ->> num1: add(num2)
    num1 ->> sum: newReal=3, newImaginary=7

In the sequence diagram above, we have num1 and num2 representing two complex numbers being added, and sum representing the result of the addition.

Conclusion

In Java, you can override operators for custom classes by using method overloading. By defining specific methods for operators like +, -, *, /, etc., you can customize the behavior of operators for your classes. This allows you to create more expressive and readable code when working with custom types. Remember to follow the conventions and signatures for method overloading to effectively override operators in Java.