Polymorphism

The linguistic roots of the term polymorphism can be translated into “many meanings”. In programming terms polymorphism is where multiple methods of the same names have different meanings. Selecting method names is an important skill for a programmer, choosing good names can make a big difference to how easy it is to understand code. For this reason programmers have developed naming conventions so that methods have similar names for similar purposes. Thus in Java there is the JavaBeans naming convention that cover frequently recurring tasks that methods are expected to perform. This takes the form of what are called get and set methods e.g.

getFirstName

setFirstName

An experienced programmer will immediately recognise these methods as “accessor” and “mutator” methods. Thus getFirstName will return information on a field called firstname and setFirstName will set the value of the firstname field. A programmer who was not familiar with the JavaBeans naming convention might reasonably have created methods called findFirstName and updateFirstName, but adhering to the established naming convention gives other programmers a slight head start in understanding the code.

As most programming involves working with existing code, ease of understanding is an essential attribute of good code. One of the ways polymorphism is implemented in Java is via method overloading. This is where two methods have the same name but different argument types.

If you have two methods with the same name, there is a need to distinguish between the different versions. In java this is done via the argument list to the method. Thus for example you could have

public update(String sFirstName)
public update(double dSalary)

Note how this shows two methods with the same name but different parameter types. Thus any call to the method update will have a different meaning, depending on the type of parameter passed to it. Thus a String parameter will call the first version, whereas the second will call the double version. This is mainly of use to programmers as it eases the burden of memorising method names. From a computer/compiler perspective it would as efficient to have two different methods, perhaps one called updateName and the other called updateSalary. In a small program having two methods would not be a problem, but in an industrial sized program with multiple programmers it places a slight additional burden on the attention of programmers and the need for documentation.

Example PolyUpdate

public class PolyUpdate{
private String sFirstName;
private double dSalary;
public static void main(String argv[]){
new PolyUpdate();

}
/** Constructor for the class */
PolyUpdate(){
/*Note how the same method name is used to
call two versions of update method */
update("james");
update(10.1);

}
public void update(String sFirstName){
this.sFirstName = sFirstName;
}

public void update(double dSalary){
this.dSalary=dSalary;
}
}



In this example an anonymous instance of the program is created with the call

new PolyUpdate();

Within the constructor there are two calls to update, one receives a string and one a double value. Java knows which version to call based on the data type passed to the method. Note that the names of the parameter do not make any difference only the type and order. Thus it would be considered overloading if you had two methods as follows.

Overloaded methods are distinguished by the argument type and order

public void update(double dSalary, String sFirstName){
/* method body goes here */
}

public void update(String sFirstName, double dSalary){
/* method body goes here */
}


Last modified: Thursday, 24 July 2014, 2:54 PM