Learning Objectives
After completing this session, you will be able to:
‰ Explain method overloading
Method Overloading
Method overloading:
‰ Allows a method with the same name but different parameters, to have different
implementations and return values of different types
‰ Can be applied when the same operation has different implementations
‰ Always remember that overloaded methods have the following properties:
‰ The same method name
‰ Different parameters or different number of parameters
‰ Return types can be different or same
Example of Method Overloading
public void print( String temp ){
System.out.println("Name:" + name);
System.out.println("Address:" + address);
System.out.println("Age:" + age);
}
public void print(double eGrade, double mGrade,
double sGrade)
System.out.println("Name:" + name);
System.out.println("Math Grade:" + mGrade);
System.out.println("English Grade:" + eGrade);
System.out.println("Science Grade:" + sGrade);
}
public static void main( String[] args )
{
StudentRecord annaRecord = new StudentRecord();
annaRecord.setName("Anna");
annaRecord.setAddress("Philippines");
annaRecord.setAge(15);
annaRecord.setMathGrade(80);
annaRecord.setEnglishGrade(95.5);
annaRecord.setScienceGrade(100);
//overloaded methods
annaRecord.print( annaRecord.getName() );
annaRecord.print( annaRecord.getEnglishGrade(),
annaRecord.getMathGrade(),
annaRecord.getScienceGrade());
}
Program Output
You will have the output for the first call to print:
Name:Anna
Address:Philippines
Age:15
You will have the output for the second call to print:
Name:Anna
Math Grade:80.0
English Grade:95.5
Science Grade:100.0
Try It Out
Problem Statement:
Write a program that illustrates invoking overloaded methods.
Code:
class TestAdder {
public int addThem(int x, int y) {
System.out.println("Inside addThem(int x, int y) method...");
return x + y;
}
// Overload the addThem method to add doubles instead of ints
public double addThem(double x, double y) {
System.out.println("Inside addThem(double x, double y)
method...");
return x + y;
}
}
//From another class, invoke the addThem() method
public class TestAdder {
public static void main(String[] args) {
Adder a = new Adder();
int b = 27;
int c = 3;
int result = a.addThem(b, c); // which addThem is invoked?
double doubleResult = a.addThem(22.5, 9.3); // which addThem?
}
}
Refer File Name: TestAdder.javato obtain soft copy of the program code
How It Works:
‰ In the TestAdder program, the first call toa.addThem(b,c) passes two ints to the
method.
‰ So, for the above call, the first version of addThem() – the overloaded method that
contains two int arguments – is done.
‰ The second call to a.addThem(22.5, 9.3) passes two doubles to the method.
‰ So, for the above call, the second version of addThem() – the overloaded method that
contains two double arguments – is done.
Tips and Tricks:

Summary
‰ Methods can be overridden or overloaded;constructors can be overloaded but not
overridden.
‰ Overloading means reusing a method name, but with different arguments.
‰ Overloaded methods:
oMust have different argument lists
oMay have different return types, if argument lists are also different
oMay have different access modifiers
oMay throw different exceptions
‰ Methods from a superclass can be overloaded in a subclass.
‰ Reference type determines which overloaded method will be used at compile time.
After completing this session, you will be able to:
‰ Explain method overloading
Method Overloading
Method overloading:
‰ Allows a method with the same name but different parameters, to have different
implementations and return values of different types
‰ Can be applied when the same operation has different implementations
‰ Always remember that overloaded methods have the following properties:
‰ The same method name
‰ Different parameters or different number of parameters
‰ Return types can be different or same
Example of Method Overloading
public void print( String temp ){
System.out.println("Name:" + name);
System.out.println("Address:" + address);
System.out.println("Age:" + age);
}
public void print(double eGrade, double mGrade,
double sGrade)
System.out.println("Name:" + name);
System.out.println("Math Grade:" + mGrade);
System.out.println("English Grade:" + eGrade);
System.out.println("Science Grade:" + sGrade);
}
public static void main( String[] args )
{
StudentRecord annaRecord = new StudentRecord();
annaRecord.setName("Anna");
annaRecord.setAddress("Philippines");
annaRecord.setAge(15);
annaRecord.setMathGrade(80);
annaRecord.setEnglishGrade(95.5);
annaRecord.setScienceGrade(100);
//overloaded methods
annaRecord.print( annaRecord.getName() );
annaRecord.print( annaRecord.getEnglishGrade(),
annaRecord.getMathGrade(),
annaRecord.getScienceGrade());
}
Program Output
You will have the output for the first call to print:
Name:Anna
Address:Philippines
Age:15
You will have the output for the second call to print:
Name:Anna
Math Grade:80.0
English Grade:95.5
Science Grade:100.0
Try It Out
Problem Statement:
Write a program that illustrates invoking overloaded methods.
Code:
class TestAdder {
public int addThem(int x, int y) {
System.out.println("Inside addThem(int x, int y) method...");
return x + y;
}
// Overload the addThem method to add doubles instead of ints
public double addThem(double x, double y) {
System.out.println("Inside addThem(double x, double y)
method...");
return x + y;
}
}
//From another class, invoke the addThem() method
public class TestAdder {
public static void main(String[] args) {
Adder a = new Adder();
int b = 27;
int c = 3;
int result = a.addThem(b, c); // which addThem is invoked?
double doubleResult = a.addThem(22.5, 9.3); // which addThem?
}
}
Refer File Name: TestAdder.javato obtain soft copy of the program code
How It Works:
‰ In the TestAdder program, the first call toa.addThem(b,c) passes two ints to the
method.
‰ So, for the above call, the first version of addThem() – the overloaded method that
contains two int arguments – is done.
‰ The second call to a.addThem(22.5, 9.3) passes two doubles to the method.
‰ So, for the above call, the second version of addThem() – the overloaded method that
contains two double arguments – is done.
Tips and Tricks:

Summary
‰ Methods can be overridden or overloaded;constructors can be overloaded but not
overridden.
‰ Overloading means reusing a method name, but with different arguments.
‰ Overloaded methods:
oMust have different argument lists
oMay have different return types, if argument lists are also different
oMay have different access modifiers
oMay throw different exceptions
‰ Methods from a superclass can be overloaded in a subclass.
‰ Reference type determines which overloaded method will be used at compile time.
No comments:
Post a Comment