You override a method when you replace the implementation of an inherited method from a base class in a subclass of it. The overridden method in the subclass has to have the same method signature (name, parameters, modifiers) than the method inherited from the base class.
Overloading a method means that you create a method with the same name as another method in the same class but with different type or number of parameters.
Yes, Java 1.5 introduced covariant return types! But I'm quite sure you are only allowed to use them for overridden methods not overloaded methods.
For explanation to Shumani: Covariant return types mean that when you override a method in a subclass you are allowed to change the return type to a derived class of the return type used by the superclass method.
And to cause some more confusion there are more subtleties regarding overriding methods. You are even allowed to widen the access modifier of a method. So you can override a protected method from a superclass with a public method in the subclass. But you're just allowed to widen access modifiers not to narrow them because otherwise the public API of a class could become invalid in subclasses.
[ March 28, 2008: Message edited by: Marco Ehrentreich ] [ March 28, 2008: Message edited by: Marco Ehrentreich ]
There's one more thing I've forgotten regarding overridden methods...
Overridden methods are allowed to throw any additional RUNTIME exceptions the base class method doesn't throw.
And they are allowed to throw less checked exceptions or subtypes of the checked exception(s) the overridden method in the superclass declares in its throws clause. But you are not allowed to override it with a broader or totally different type of checked exception.
Overloaded methods have different argument list,can have different return types,can have different acess modifiers and can declare border checked exceptions. Overridden methods must have same argument list,same return type,access level should not be more restrictive.These methods can be inherited.one cannot override a method if it is marked final or static.
Joined: Apr 16, 2004
I would like to say that, its the method calls which determine the ambiguity of overloaded methods.
Here is a small overview which types are implicitly casted by Java to another type. As you see implicit casts are only possible up the hierarchy. Otherwise you would there could be a potential loss in precision so you have to force the compiler to explicitly cast a type down the hierarchy (not so good idea unless absolutely necessary).
If you use numeric literals like "2.0" Java automatically chooses an adequate type for it internally which is double in this case. In this case the compiler simply doesn't know that 2.0 could be cast to an int without loss of precision. That are the rules!