Previous |
Next |
The terms overloaded and overridden are similar enough to give cause for confusion. It is a fairly typical Java programmer job interview to be asked to described the difference between the two terms. My way of remembering it is to imagine that something that is overridden has literally been ridden over by a heavy vehicle and no longer exists in its own right. Something that is overloaded is still moving but is loaded down with lots of functionality that is causing it plenty of effort. This is just a little mind trick to distinguish the two, it doesn't have any bearing of the reality on the operations in Java.
Overloading
Method names are a big deal in programming. Remembering what a method name does is part of the mental work every programmer has to do. It is because of this that programming projects establish naming conventions so that people can usually guess what the name of a method is likely to be. Thus the JavaBeans convention has the get/set convention. It is a source of immense frustration when a method has a name that isn't obvious. Overloading tries to make it possible to use the same method name for similar tasks. For example if you had a method for calculating the area within a square called
calcArea(Square mysquare)
it would be very useful if you could use the same method name for calculating the area of a circle. The problem is that you have already “used up the name” with a method that takes a Square parameter. With overloading you can have two methods with the same name, but that take a different parameter type. Java will work out which method should be called according to the parameter type. Thus if you create a method with the signature
calcArea(Circle mycircle)
It will not cause an error and will distinguish which method to call according to the parameter. The following code demonstrates that all that is needed to overload a method is that the parameter types are different. It is the types that matter, not the names. If you compile and run this code both methods will be called correctly according to the data types passed to them. The same principle works where the parameter types are classes (as illustrated in the previous calcArea method).
public class OverLoad{
int i=10;
byte j =99;
public static void main(String argv[]){
OverLoad ol = new OverLoad();
}
OverLoad(){
amethod(i,j);
amethod(j,i);
}
public void amethod(int i, byte j){
System.out.println("int, byte");
}
public void amethod(byte j, int i){
System.out.println("byte, int");
}
}Overriding
Overriding is a term related to inheritance. A method is overridden in a subclass when a substitute method of exactly the same name, parameters and return types is implemented and thus replaces the version in the parent class. With an overridden method its entire functionality has been replaced in the child class.
Overriding is useful in that it allows a programmer to inherit most of the functionality of one class but to change that functionality slightly by replacing one or more of the methods in the child class. The new (overridden) method will appear to be exactly the same to any other programmer as it will have the same signature, i.e. Name, parameter types and return type, but what it actually does, its functionality will be different. This important in that it allows the programmer to keep the same interface, the outward appearance whilst changing the inner workings.
The core Java language includes an example of overriding starting with the great grandparent Object class. Every object in Java is implicitly a child (or ancestor at least) of the Object class. The Object class has a method called equals, which in the default implementation will compare the memory address of two different classes. This is rarely what is wanted. So if you had two String objects you would probably want the equals method to compare the sequence of characters within each object. So it is no surprise that the Java String object does indeed override the equals method with a new version that makes a character by character comparison. Overriding and runtime type is an essential component of runtime polymorphism, the selection of the method to execute on the basis of the Object rather than the type of the reference to the object. This is described in much greater detail in chapter 6.
The Sun tutorial on overriding
http://java.sun.com/docs/books/tutorial/java/IandI/override.html
Overriding according to Tolliver and Lindblad
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter04/overriding.html
Overloading according to Bruce Eckel
http://www.faqs.org/docs/think_java/TIJ306.htm#Heading3424
Previous |
Next |