0% found this document useful (0 votes)
20 views

3 Method Overloading

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

3 Method Overloading

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

METHOD OVERLOADING

INTRODUCTION TO JAVA PROGRAMMING

CSA6003T

BHAWANA BOTHARA

ASSISTANT PROFESSOR
LEARNING OBJECTIVES

• Define method overloading and understand its purpose.


• Identify the rules for method overloading in Java.
• Implement method overloading with examples.
• Learn how method overriding works and its significance in Java.
• Differentiate between method overloading and overriding.
INTRODUCTION

• In Java, it is possible to define two or more methods within the same class
that share the same name, as long as their parameter declarations are
different. When this is the case, the methods are said to be overloaded, and
the process is referred to as method overloading.
• Method overloading is one of the ways that Java supports polymorphism.
• Method overloading is an example of compile time polymorphism.
WHY DO WE NEED OVERLOADING
• If we have to perform only one operation, having same name of the methods
increases the readability of the program.
• Suppose you have to perform addition of the given numbers but there can be
any number of arguments, if you write the method such as a(int,int) for two
parameters, and b(int,int,int) for three parameters then it may be difficult for
you as well as other programmers to understand the behavior of the method
because its name differs.
DIFFERENT WAYS TO OVERLOAD THE
METHOD

• There are two ways to overload the method in java


• Changing the Number of Parameters.
• Changing Data Types of the Arguments.
• Changing the Order of the Parameters of Methods
1. OVERLOADING BY CHANGING THE
NUMBER OF PARAMETERS
class MethodOverloading
{
private static void display(int a){
System.out.println("Arguments: " + a);
}
Arguments: 1
private static void display(int a, int b){ Arguments: 1 and
System.out.println("Arguments: " + a + " and " + b);
}
4

public static void main(String[] args) {


display(1);
display(1, 4);
}
}
2. CHANGING DATA TYPES OF THE
ARGUMENTS
class MethodOverloading
{

// this method accepts int


private static void display(int a){
System.out.println("Got Integer data.");
}

// this method accepts String object Got Integer data.


private static void display(String a){ Got String object.
System.out.println("Got String object.");
}

public static void main(String[] args) {


display(1);
display("Hello");
}
}
3. CHANGING THE ORDER OF THE
PARAMETERS OF METHODS
class Student {
public void StudentId(String name, int roll_no)
{
System.out.println("Name :" + name + " "
+ "Roll-No :" + roll_no);
}
public void StudentId(int roll_no, String name)
{
System.out.println("Roll-No :" + roll_no + " "
+ "Name :" + name);
}
}
class GFG {
public static void main(String[] args)
{
Student obj = new Student();
obj.StudentId("Spyd3r", 1);
obj.StudentId(2, "Kamlesh");
}
}
WHAT IF THE EXACT PROTOTYPE DOES NOT
MATCH WITH ARGUMENTS?
• Priority-wise, the compiler takes these steps:
• Type Conversion but to a higher type(in terms of range) in the same family.
• Type conversion to the next higher family(suppose if there is no long data type available
for an int data type, then it will search for the float data type).
OVERLOADING CONSTRUCTORS
• In addition to overloading normal methods, you can also overload
constructor methods. In fact, for most real-world classes that you create,
overloaded constructors will be the norm, not the exception.
class Box {
double width; class OverloadCons
double height; {
double depth; public static void main(String args[])
// constructor used when all dimensions specified {
Box(double w, double h, double d) { // create boxes using the various constructors
width = w; Box mybox1 = new Box(10, 20, 15);
height = h; Box mybox2 = new Box();
depth = d; Box mycube = new Box(7);
} double vol;
// constructor used when no dimensions specified // get volume of first box
Box() { vol = mybox1.volume();
width = -1; // use -1 to indicate System.out.println("Volume of mybox1 is " +
height = -1; // an uninitialized vol);
depth = -1; // box // get volume of second box
} vol = mybox2.volume();
// constructor used when cube is created System.out.println("Volume of mybox2 is " +
Box(double len) { vol);
width = height = depth = len; // get volume of cube vol = mycube.volume();
} System.out.println("Volume of mycube is " +
double volume() { vol);
return width * height * depth; }
} }
}
COMMON MISTAKES IN METHOD
OVERLOADING

• Misunderstanding Return Type: Overloading is not based on different return


types.
• Incorrect Parameter Types: Methods must have different parameter types or
numbers, not just names
• .Overuse: Overloading can lead to confusion if used excessively
ADVANTAGES OF METHOD
OVERLOADING
• Method overloading improves the Readability and reusability of the program.
• Method overloading reduces the complexity of the program.
• Using method overloading, programmers can perform a task efficiently and
effectively.
• Using method overloading, it is possible to access methods performing related
functions with slightly different arguments and types.
• Objects of a class can also be initialized in different ways using the constructors.
SUMMARY

• Method overloading in Java is a powerful feature that enables a class to have multiple methods
with the same name but different parameter lists, enhancing code readability and usability. By
allowing methods to differ by the type, number, or order of parameters, overloading facilitates
the handling of various data inputs under a single method name. This technique exemplifies
compile-time polymorphism, improving code flexibility and reusability. Key rules include
maintaining the same method name and ensuring different parameter lists, while return type
does not determine overloading. Practical applications, such as overloading methods in an ATM
system for different withdrawal scenarios, highlight its real-world benefits. Overall, method
overloading streamlines code management and increases functionality without sacrificing
clarity.
ASSESSMENT
1. Which of the following is true about method overloading?
A. Same parameter types.
B. Different method names.
C. Different parameter lists.
2. Method overloading is an example of ________ polymorphism.
Answer: Compile-time
3. In method overloading, methods must have the same ______ but different ________.
Answer: Name, parameters
4. True or False: Method overloading can have different return types.
Answer: True
ASSESSMENT
5. Which of the following cannot be used to differentiate overloaded methods?
A. Number of parameters
B. Type of parameters
C. Order of parameters
D. Return

6. Method overloading improves code ________.Answer: Readability


7. Method overloading is resolved at ______ time.Answer: Compile
8. True or False: Method overloading requires inheritance.Answer: False
9. Which of the following is not a rule for method overloading?
A. Different parameter lists
B. Same method name
C. Different return types
ASSIGNMENT

1. Explain method overloading with a real-life example and provide a Java


code implementation.
2. What are the benefits of method overloading in Java? Discuss with
examples.
3. Can we overload methods on return type?
4. Can we overload static methods?
5. Explain different ways of function overloading in JAVA.

You might also like