What is Method Overloading?

A method having the same name as another method (in same class or a sub class) but having different parameters is called an Overloaded Method.

Example 1-doIt method is overloaded in the below example:

class Foo{
public void doIt(int number){
}
public void doIt(String string){
}
}

Example 2-Overloading can also be done from a sub class.

class Bar extends Foo{
public void doIt(float number){
}
}

Java Example

Constructors
public HashMap(int initialCapacity, float loadFactor)
public HashMap()
public HashMap(int initialCapacity)

Creating a Sub Class Method with same signature as that of a method in SuperClass is called Method Overriding. Let?s define an Animal class with a method shout.

 

public class Animal {
public String bark() {
return "Don't Know!";
} }

class Cat extends Animal {
public String bark() {
return "Meow Meow";
}
}

bark method in Cat class is overriding the bark method in Animal class.

Java Example : HashMap public int size() overrides AbstractMap public int size()