Method Overloading and Overriding in Java

Overloading: To check for overloading: a method foo in class A is considered overloaded, if an object of class A can call another function with the same name, but with different "signature".

"signature" in Java consists of the function name, and the list of arguments of the function (no return type).

In the absence of overloading, I can identify the method to call without looking at the arguments.

Some examples of overloading:

Ex1 :

 
class A{
	Tx foo(T2 x){ .. } // overloaded
	Ty foo(T3 x){ ...} // overloaded
	Tz foo(T4 x, T5 y) {...} // overloaded

	// int foo(T2 x) {...} // type error -- two functions in the class cannot have the same signature
	
	// Note: T2, T3 have to be different.
	// No constraint on Tx, Ty, Tz (they can be same, different, or subtype of one another).
}

Ex2:

class A{
	Tx foo(T2 x) {...} // not overloaded
}
class B extends A {
	Ty foo(T3 x) {...} // overloaded
	// Constraint on T2 and T3: They are different (can even be subtype of one another).
	// if T2 = T3, it may be a case of overriding (see below).
	// No constraint on Tx and Ty (they can be same, different, or subtype of one another).
}
Overriding: A method in a child class overrides a method in the parent class, iff both the methods have the same name and exactly the same signature (see the definition of signature above). The return types also have to "match" (see the example below).

class A{
	T1 foo(T2 y) {...}
}
class B extends A{
	T3 foo(T2 y) {...}
}
We say that foo@B overrides foo@A, iff T3 "matches" T1. That is, either T3 = T1 or T3 is a subtype of T1. If the signatures are the same, but the return types do not "match" then it would be a type error.