Although the subject of overloading and overriding might seem trivial to experienced java developers it is still quite confusing.
In this tutorial we will see what is the meaning of overloading and overriding.

Overloading
Overloading occurs when there are two methods with the same name but with different arguments type or argument list.
As an example consider the following cases:
Two methods with the same name but different argument type

 

pulic void aMethod(String anArgument){
//do something useful with the argument
}

public void aMehtod(Object anArgument){
//do something useful with the argument
}

Two methods with the same name but different argument list

public void aMethod(String anArgument){
//do something usefull
}

public void aMethod(String anArgument,Integer anotherArgument){
//do somethig usefull
}

 The concept of overloaded methods is used extensively in Java and you can find overloaded methods in many build-in classes.
For example,one of the overloaded methods of String class is subString().Substring() method arguments can be either the starting index of the substring
or the starting and the ending index of the substring.In other words,substring method can be:

public String subString(int startIndex){
//useful code goes here
}
public String subString(int startIndex,int endIndex){
//useful things happen here
}

 Overriding

Overriding  occurs when  a  method in a subclass has the same name and same signature as a method in the super class but different implementation.
As an example consider the following case:

//the superclass
public class SuperClass{
//useful code goes here
public void aMethod(){
System.out.println("A method in the super class");
}
}


public class Subclass extends SuperClass{
//useful code goes here

public void aMethod(){
System.out.println("A method in the subclass");
}
}

Concerning method overriding some things good to remember are:


 1)Static and final methods cannot be overridden: Static methods cannot be overridden because are class methods.In other words static methods belong to the class not to objects
 On the other hand final methods are marked as final  in cases where the method does something important and must not be overridden in any subclass etc
 2)An overridden method cannot throw broader exceptions that the orginal method.
 3)An overriden method must have the same name and same signature as the original method
 4)The overriden method must not have narrower access modifier than the original method.In other words if the original method is declared as public theoverridden method cannot be "private"