Copyright

Overloading in Java: Methods & Constructors

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Unlike a semi-trailer, an overloaded Java method or constructor isn't necessarily a bad thing. Java allows overloading. It is a useful tool in the Java programmer's toolbox. This lesson will explain the concept and provide working code examples.

Overload!

The good news is that this overload concept won't break up the highway. In Java, the term overload means that there are multiple versions of a constructor or method. They will each have a different number of arguments or values that they take in to work with.

For example, a payroll program could have an Employee class, and constructors that create Employee objects of varying types. One constructor might take in employee name, ID, and state code; another might not need any arguments and just create an Employee.

So what does overloading really look like in code?

Overloaded Constructors

Here is an example of an overloaded constructor. The class Conversion has TWO constructors, one that takes a conversion rate and the other also has a modifier option.

public Conversion {
  public double conversionRate;
  public double modifier;
  // constructor here:
  public Conversion(double c) {
   conversionRate = c;
  }
  //another constructor: the overload
  public Conversion(double c, double m) {
   conversionRate = c;
   modifier = .00587;
  }
}

Now that we've created the constructors, how do we actually use them?

Using Overloaded Constructors

A constructor is the main engine that is used when you create a new instance of a class. If you have overloaded your constructors, Java will know which constructor to use based on the number of arguments.

//Create a new instance
Conversion conversion1 = new Conversion(48.056465584);
//Or, use two doubles
Conversion conversion2 = new Conversion(43.23, 388.888);

Java knows which constructor to use based on the number of arguments.

We've overloaded constructors, the main engine of classes. Now let's look at overloading methods, the blocks of code that carry out instructions.

Overloaded Methods

A method is a set of code that can take arguments, that is values, and do something with those values. For example, a method in a payroll program could multiply wages by hours worked. The method then returns the value it comes up with and this value is sent back to the line of code that called the method.

You can overload a method in the same way as a constructor. That is, multiple methods can exist, each with the same name. Remember, though, that they can't have the same number of arguments or the same type of arguments! Overloading isn't cloning.

In the following code example, we build upon our program and add in methods for performing the conversion. One takes a single value, the other two values:

public double setRate(double c) {
  conversionRate = 352.222;
  return conversionRate;
}
public double setRate(double c, double m) {
  conversionRate = 352.22;
  modifier = .0035;
  double newRate = conversionRate * modifier;
  return newRate;
}

Overloaded Methods: Same Arguments, Different Types

A constructor can't be cloned (same name, same number of arguments). The same is true for a method, EXCEPT that you can have the same number of arguments but with different types of data.

The following methods have the same number of arguments, but the types are different. It may be a little confusing, but there could be times when multiple methods of the same type are needed. If so, document your code!

public static int addTotals(int counter, int bonusCount) {
  return counter + bonusCount;
}
public static long addTotals(long bigCount, long bonusPlus) {
  return bigCount + bonusPlus;
}

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support