Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

What's the difference between overloading a method and overriding it in Java?

Is there a difference in method signature, access specifier, return type, etc.?

share|improve this question
In PHP overloading has a completely different meaning. – lortabac Sep 10 '12 at 16:43

4 Answers 4

To overload a method with a new method, the new method should have a different signature. I.e. two overloaded methods have the same name, but different parameters. Here's an example of two overloaded methods:

boolean isOdd(int number) { ... };
boolean isOdd(float number) { ... };

Based on the parameter types, the corresponding method will be called. Note that changing the return type is not enough (though you can do this additionally).

When a method is overridden, then the new method has the same signature and replaces the overridden method in some cases. Here's an example of an overridden method:

public class A 
     public void someMethod() { ... }

public class B extends A
     public void someMethod() { ... }

The choice is made based on the object type. For example,

A someA = new B();

will call the someMethod of B. You can (and should) add the @Override annotation:

public class B extends A
     public void someMethod() { ... }

Now, if you accidentally change the parameters in B, the compiler will inform you, that you are not overriding someMethod() but overloading it.

share|improve this answer
@scarfridge, thanks for the edit. Haven't been programming with Java for ages and forgot a lot ))) – superM Sep 10 '12 at 18:30
Thanks for mentioning the @Override annotations and the fact that having only different return types does not cause a method to override, but instead to overload. – Dadinn Nov 12 at 16:36

Overloading, methods have the same name but different parameters.

Overriding, the implementation given in base class is replaced with that in sub class.

share|improve this answer
+1 for simplicity – Andy Nov 13 '13 at 8:20
please do not edit/expand this. The brevity is point. – NimChimpsky Mar 5 '14 at 15:01

Concepts you ask about are covered in Java tutorials.

Explanation for overriding is given as follows:

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.

The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method it overrides. An overriding method can also return a subtype of the type returned by the overridden method. This is called a covariant return type.

When overriding a method, you might want to use the @Override annotation that instructs the compiler that you intend to override a method in the superclass. If, for some reason, the compiler detects that the method does not exist in one of the superclasses, it will generate an error. For more information on @Override, see Annotations...

Overloading is explained in tutorial as follows:

The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists (there are some qualifications to this that will be discussed in the lesson titled "Interfaces and Inheritance".

Suppose that you have a class that can use calligraphy to draw various types of data (strings, integers, and so on) and that contains a method for drawing each data type. It is cumbersome to use a new name for each method—for example, drawString, drawInteger, drawFloat, and so on. In the Java programming language, you can use the same name for all the drawing methods but pass a different argument list to each method. Thus, the data drawing class might declare four methods named draw, each of which has a different parameter list...

Overloaded methods are differentiated by the number and the type of the arguments passed into the method...

You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.

The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.

Note: Overloaded methods should be used sparingly, as they can make code much less readable.

Above explanation for overloading mentions qualifications discussed in the lesson titled "Interfaces and Inheritance":

In a subclass, you can overload the methods inherited from the superclass. Such overloaded methods neither hide nor override the superclass methods—they are new methods, unique to the subclass.

share|improve this answer

Overloading a method is typically defined as "providing multiple available methods with the same name, differing by the number and type of inputs and outputs". The concept is usually that you want to be able to perform the same basic operation given varying sets of inputs: you can, for instance, "add" any two values of numeric type, however it is usually important to know what the exact type of the value is so that you can take advantage of or plan for specific behaviors of that type. As such, you would define a method for each combination of numeric types (and or collections) you wish to support. All these methods have the same name, but different "signatures"; at compile-time, the compiler will match a call to a particular method name with a given combination of inputs to a specific pointer in the application memory used to store code.

Overriding a method is typically defined as "providing a different implementation in a derived class of a method with a particular signature defined in the base class". There are many reasons to override a method; virtually all of them have in common the fact that the derived class has additional knowledge of what must be done, which cannot be known by the base class. There are two flavors of overriding in most OO languages; overriding can replace the base class method, or it can extend the base class method. The difference is usually that a derived class that is extending a base class implementation will call the base class's overridden version of the method at some point during the execution of the overriding method. This allows overriding classes to "re-use" common areas of the operation which are contained in the base class.

share|improve this answer

protected by Community Mar 5 '14 at 13:59

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.