Use method overloading in Java
Takeaway: Devising unique naming conventions can be a tedious chore, but reusing method names via overloading can make the task easier. Here's a look at how this technique works in Java.
Naming conventions are an important aspect of any development project, but coming up with unique names can be somewhat tedious. One way to simplify the chore is to reuse method names via overloading. Overloading is the ability to have a class that has multiple methods with the same name that are differentiated by the number and type of arguments passed into the method.
What's in a name?
When assigning names to classes, methods, and member variables, it is important to use names that are easy to understand. For example, creating a class for defining a person would lead to an appropriate class name of Person. Calling it a gibberish name like dkjfb would be valid Java code but absolutely nonsense to any programmer working on the system in the future. The Person class has the following designation:
class Person {
private String firstName;
private String lastName;
}
The code listing declares a class called Person, with two member variables to store first and last names. The names assigned to the member variables are analogous to their use, so it is easy to identify what type of data is stored in them. When developing the Person class, we need to populate these member variables when the object is created.
Object construction
Creating a new object instance triggers the class's constructor method. The following code utilizes a basic constructor that accepts no arguments:
class Person {
private String firstName;
private String lastName;
Person() {
this.firstName = "";
this.lastName = "";
} }
The basic constructor initializes the member variables with empty strings. Upon further investigation, it is discovered that often the objects are created with known names but sometimes only the last name. You can use method overloading to create multiple method versions, but each has its own method signature. The signature defines what parameters are accepted by the method. For example, here's the method signature for the previous constructor:
Person()
This method can be overloaded to accommodate first and last names or only the last name:
class Person {
private String firstName;
private String lastName;
Person() {
this.firstName = "";
this.lastName = "";
}
Person(String lname) {
this.firstName = "";
this.lastName = lname;
}
Person(String fname, String lname) {
this.firstName = fname;
this.lastName = lname;
} }
Any two methods with the same name in a class must have different parameter types or a different parameter count; if not, the compiler will reject them. The class may now be declared as follows:
Person p1 = new Person();
Person p2 = new Person("Patton");
Person p3 = new Person("Patton", "Tony");
A current Java feature
Overloading is used in the standard Java classes. The System.out.println method accepts multiple parameter lists. This sample code snippet is valid:
System.out.println("Builder.com");
as is this one:
int test = 2;
System.out.println(test);
Both code snippets compile and execute with no problems. The println method has been designed to accept varying parameters, so overloading is applicable beyond constructors. To further illustrate this, we'll extend our sample class by adding a print method to output the first and last names:
class Person {
private String firstName;
private String lastName;
Person() {
this.firstName = "";
this.lastName = "";
}
Person(String lname) {
this.firstName = "";
this.lastName = lname;
}
Person(String fname, String lname) {
this.firstName = fname;
this.lastName = lname;
}
public void Print() {
System.out.println(firstName + " " + lastName);
}
public void Print(String pout) {
System.out.println(pout + " " + firstName + " " + lastName);
}
}
These two print methods output the member variables, with one method accepting text to prepend to the output and the other not.
When to use overloading
Overloading is a powerful feature, but you should use it only as needed. Use it when you actually do need multiple methods with different parameters, but the methods do the same thing. That is, don't use overloading if the multiple methods perform different tasks. This approach will only confuse other developers who get a peek at your code.
Print/View all Posts Comments on this article
SponsoredWhite Papers, Webcasts, and Downloads
- Network Readiness For VoIP ShoreTel
- Webcast: How to Get the Most Out of Microsoft Windows Deployments with Intelligent iSCSI Storage Dell EqualLogic
- Next Generation Mobility Now Sprint
- Antivirus Software and Disk Defragmentation Diskeeper
- Enhancing Desktop and Laptop Security Performance with Disk Defragmentation Diskeeper
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET
