What is overloading in Java?

I do not understand overloading in Java. Is there a connection with polymorphism? It seems to me very abstract. Am I coming more with Javascript? Will this be applied in javascript?

+3
java javascript
Oct 29 '12 at 14:05
source share
4 answers

Overloading means that the same method name can be defined with several signatures; list of formal parameters (and their types).

Overloading means different things in different languages. Java is strongly typed (some may say "fiercely typed" in fact). It just means that there may be different versions of the function, and the compiler can determine which one is intended by considering the types of parameters when calling the function (method).

JavaScript is not like that; formal parameters for a function are just links by name in the function body, but otherwise there is nothing special in them, and any function can be called with any arguments and any number of them.

Some languages ​​use "overload" as a concept of runtime. In Erlang, these are not argument types that matter when choosing from several alternative options for a function; these are values.

edit - @MarkoTopolnik indicates that the problem is not so much in the "strength" (or "ferocity" :-) of the type of system as in its static nature. Java insists that types are explicitly declared almost everywhere, while JavaScript (with the exception of some new typed array constructs) does not work.

+12
Oct 29
source share

A method that has the same name with multiple definitions is known as overloading.

may differ from the type of argument and the number of arguments.

Example

import java.io.*; class demoOverloading { int add(int x,int y) //method definition { return (x+y); } double add(double x,double y) //method definition { return (x+y); } } public class JavaApplication4 { public static void main(String[] args) { demoOverloading demo=new demoOverloading(); //creating object for above class System.out.println("Sum of Integer "+demo.add(10,20)); //calling integer add method System.out.println("Sum of double "+demo.add(33.44,67.5)); //calling double add method } } 

In the above example, having two methods with the same name, add , but one contains int and the other contains double arguments, as you can do with the other argument,

A difference in the number of arguments is also possible.

thank

+1
Oct 29 '12 at 14:13
source share

Overloading is a function that allows you to have 2 methods with the same name and a different signature in the same class, for example

 public void foo(); public void foo(int i); 

Now you can call the foo() method with no arguments or with a single int argument, and various methods will execute.

You are probably confused with overloading and overriding. Change is indeed associated with polymorphism. This is the ability to redefine (change) the functionality of a base class to a subclass. For example, if Child extends Base and both have a foo() method, the child foo() overrides the Base implementation. A similar function does exist in JavaScript.

+1
Oct 29
source share

if you have the following method in your class

public void calculate() {}

Listed below are some overloaded versions. (Pay attention to the same name)

public void calculate(int i) {}
public void calculate(int i, int j) {}

But the following is not overloaded with one of the above methods. This method differs from the original method only in that it returns a return type. Methods with the same signature but with different types of return values ​​are not allowed in java.

public int calculate(int i) {}

+1
Oct 29 '12 at 2:31 on
source share



All Articles