Java for Beginners 5 – Operator, Method and Constructor Overloading

The following article explains why Java does not support operator overloading and looks at the exception of overloading strings in Java and outlines other concepts, such as method overloading.

Operator Overloading – History

In computer science, operator overloading, less commonly known as ‘operator ad-hoc polymorphism’ describes a state where depending on the type of argument, operators can exhibit altered behavior. The term ‘operator overloading’ got popular likely because it adequately describes the fact that a single operator can handle more than one argument.

In the 1960s, ALGOL 68 became the first language supporting overloaded operators. The similar approach was later and more elegantly adopted by C++, C#, PHP, Python, and others.

The following example shows that C++ developers can overload the function of operators such as ‘++,’ creating a contra-intuitive functionality (Jarosciak, 2018a) that instead of expected increasing, will decrease the defined data member.

Jarosciak, J. (2018a) C++ Overloading. Unpublished figure

While the use of operator overloading improves flexibility by changing the standard operator semantics, it can also become unclear and hard to understand or debug, primarily by developers who inherit such code.

Operator Overloading in Java

Operator overloading became a source of criticism in C++, and Java language architects decided to omit it from the language. Sun Microsystems clarifies the decision in comp.lang.java FAQ, stating that operator overloading was not implemented for the following reason: “Because C++ has proven by example that operator overloading makes code almost impossible to maintain.” (Cafeaulait.org., 2018).

The principal motive to omit operator overloading was to keep the Java code simple, maintainable and readable. Java became one of the first languages to exclude overloading, opening the door for other languages that followed the lead, such as Go or Objective-C.

Oracle explains the architectural goals of Java in the following statement: “Although Java is superficially similar to C and C++, Java gained its simplicity from the systematic removal of features from its predecessors.” (Oracle.com, 2018).
“Eliminating operator overloading leads to great simplification of code.” (Gosling and McGilton, 1995).

Some critics of excluding overloading from Java state that in alternative arithmetic, operator overloading can lead to readable code and that “Java needs to overload the arithmetic operators, the comparison operators, and the assignment operator.” (Boisvert et al., 2001). However, this never became the mainstream opinion.

Java String Overloading Exception

Java provides overloading exception for string additive operators such as + and +=.

If any + operator is a string, it results in string operation, otherwise if it’s number it generates number addition.

The example of string overloading is illustrated in Jarosciak (2018b), where Java compiler converts integer ‘value’ to String and appends it to existing string value.

Jarosciak, J. (2018b) Java String Overloading. Unpublished figure

 

Method Overloading

Method overloading is also a topic we should discuss. Before we do so, I want to note, that method overloading almost followed the same fate as operator overloading.

According to a book of Java Questions & Answers 2013-2014 Edition by Harry Choudhary (Matrix Research Labs), “C++ has proven by example that operator overloading makes code almost impossible to maintain. In fact there very nearly wasn’t even method overloading in Java, but it was thought that this was too useful for some very basic methods like print(). Note that some of the classes like DataOutputStream have un-overloaded methods like writeInt() and writeByte().”  (Choudhary, 2013). That said, it never happened and Method Overloading is supported in Java as we know it today.

To start a debate, I wanted to mention that Method Overloading is similar to Operator Overloading. As we just learned, in operator overloading, an operator can have more than one function based on the attribute used. Similarly, in Java, method overloading is a feature that permits a class to have more than one method with the same name, as long as their arguments are different among them.

“Three ways to overload a method. In order to overload a method, the argument lists of the methods must differ in either of these” (Singh et al., 2013).

These three ways are as follows:

 

1. The number of parameters.

For example, This is a valid case of overloading

add(int, int)

add(int, int, int)

 

2. The data type of parameters.

For example:

add(int, int)

add(int, float)

 

3. The sequence of the data type of parameters.

For example:

add(int, float)

add(float, int)

 

I found a definition of the term ‘Method Overloading’ that is likely the most straightforward and most precise way to describe it: “Method overloading is a process of reusing the same method name for a method that accepts different parameter types.” (Kind, 2002).

What it doesn’t say, however, is the fact that overloaded methods must have different parameters. Meaning that if we want to have the methods with the same name, having different parameters in them is not something optional, it’s a ‘must’.  It also doesn’t say that overloaded methods can have different return types. Here is a good example of method overloading in Java, illustrating how we can have methods with the same name, as long as we these methods have uniquely different parameters:

Overloading in Java, by unknown, Programcreek.com (2018)

Additionally, we should note how the Java works when it comes across a call to a method that is overloaded because that’s not immediately clear. What happens is, that when there is more than one method with the same name, Java uses the types and number of parameters as a unique identifier.

While I find the method overloading to be an exciting and useful way of programming (in some cases), not everyone is of the same opinion. The following quote summarizes the primary issues with the concept of overloading methods: “Method overloading is a controversial language feature, especially in the context of Object Oriented languages, where its interaction with overriding may lead to confusing semantics.” (Gil and Lenz, 2010).

In the next topic, we should mention the Constructor Overloading which in Java enables a class to have more than one constructor as long as they have distinctive arguments.

Constructor Overloading

Coming Soon – article in progress.

Conclusion

Java developers cannot overload ordinary operators. However, they can always declare a class with suitable variables and proper methods that operate on such variables, making overloading exclusion lesser of an issue.

 

References

Boisvert, R. F., Moreira, J., Philippsen, M., & Pozo, R. (2001). Java and numerical computing. Computing in Science & Engineering, 3(2), 18-24.

Cafeaulait.org. (2018). comp.lang.java FAQ . [online] Available at: http://www.cafeaulait.org/javafaq.html#xtocid1902938 [Accessed 3 Feb. 2018].

Gosling, J., & McGilton, H. (1995). The Java language environment. Sun Microsystems Computer Company, 2550.

Oracle.com. (2018). The Java Language Environment. [online] Available at: http://www.oracle.com/technetwork/java/simple-142339.html [Accessed 3 Feb. 2018].

Gil, J. Y., & Lenz, K. (2010, June). The use of overloading in Java programs. In European Conference on Object-Oriented Programming (pp. 529-551). Springer, Berlin, Heidelberg.

Kind, L. (2002). U.S. Patent No. 6,415,434. Washington, DC: U.S. Patent and Trademark Office.

Programcreek.com. (2018). Overloading in Java. [online] Available at: https://www.programcreek.com/2009/02/overriding-and-overloading-in-java-with-examples/ [Accessed 5 Feb. 2018].

Choudhary H. (2013). Java Questions & Answers 2013-2014 Edition. [online] Available at: https://goo.gl/eUYqat [Accessed 4 Feb. 2018].

Singh, C., Uddin, S., Gupta, R., Pawar, A., Reddy, A., NB, G., Ismail, s., Haider, M., Nji, J., Jain, D., Ranjan, R., Kaundal, R., MUALA, E., and Yadav, H. (2013). Method Overloading in Java with examples. [online] beginnersbook.com. Available at: https://beginnersbook.com/2013/05/method-overloading/ [Accessed 4 Feb. 2018].

Comments

comments