163 subscribers
3.42K photos
24 videos
39 files
371 links
Link: @java_posts

Contact: java.response.email@gmail.com
Download Telegram
Note: We have not provided any argument in the ‘parenthesis’ of the square() method in our program. In this case, the Compiler Class calls the method in which no Parameter has been defined to achieve the Argument.

output:


In this way, we can define more than one Methods of the same name in a class, which is called Method Overloading, and

The Java compiler itself performs the appropriate Method Call for an object, based on the Data Type of the Arguments of the Methods.
Benefits of using Method Overloading

Method overloading increases the readability of the program.
This provides flexibility to programmers so that they can call the same method for different types of data.
This makes the code look clean.
This reduces the execution time because the binding is done in compilation time itself.
Method overloading minimises the complexity of the code.
With this, we can use the code again, which saves memory.
How to do method Overloading ?
In java, we do method overloading in two ways:

By changing the number of parameters.
By changing data types.

Change the number of arguments:

In the example below, we have two methods, the first method has two arguments, and the second method has three arguments.
class Demo
{
void multiply(int a, int b)
{
System.out.println("Result is " + (a*b));
}
void multiply(int a, int b, int c)
{
System.out.println("Result is" + (a*b*c));
}
public static void main(String[] args)
{
Demo obj = new Demo();
obj.multiply(8.5);
obj.multiply(4, 6, 2);
}
}
output
class Sum
{
static int add(int a, int b)
{
return a+b;
}
static double add(double a, double b)
{
return a+b;
}
}
class TestOverloading2
{
public static void main(String[] args)
{
System.out.println(Sum.add(17,13));
System.out.println(Sum.add(10.4, 10.6));
}
}
Note: In this example, we are creating static methods so that we don’t need to create an instance for calling methods.

output:
Some points to remember about method overloading:


Method overloading cannot be done by changing the return type of methods.
The most important rule of method overloading is that two overloaded methods must have different parameters.

Method overloading has nothing to do with return-type

If there are two methods of the same signature within a class in the program, then Ambiguity Error comes, whether their return-type is different or not. This means that method overloading has no relation with return-type.
class Sample {
int disp(int x) {
return x;
}
double disp(int y) {
return y;
}
public static void main(String args[])
{
Sample s = new Sample();
System.out.println("value of x: " + s.disp(5));
System.out.println("value of y: " + s.disp(6.5)) ;
}
}
Output


In this way, we can define more than one Methods of the same name in a class called Method Overloading. The Java Compiler itself, based on the Data Type of the Arguments of the Methods, performs the appropriate method call for an object.
Java
Chapter 5: Object-Oriented Programming 1. Why OOP ? 2. Java Constructor 3. Accessing Parent Class Variables 4. The Java OOP Concepts 5. Abstraction 6. Encapsulation 7. Polymorphism 8. Inheritance 9. Association 10. Aggregation 11. Composition 12.…
Overriding and overloading are the core concepts in Java programming. They are the ways to implement polymorphism in our Java program. Polymorphism is one of the OOP concepts.
Overriding vs Overloading
Overriding and overloading example
Here is an example of overloading and overriding in a Java program:
package com.journaldev.examples;

import java.util.Arrays;

public class Processor {

public void process(int i, int j) {
System.out.printf("Processing two integers: %d, %d", i, j);
}

public void process(int[] ints) {
System.out.println("Adding integer array" " + Arrays.toString(ints));
}

public void process(Objects[] objs) {
System.out.println("Adding integer array: " + Arrays.toString(objs));
}
}
class MathProcessor extends Processor {

@Override
public void process(int i, int j) {
System.out.println("Sum of integer is " + (i + j));
}

@Override
public void proces(int[] ints) {
int sum = 0;
for (int i: ints) {
sum += i;
}
System.out.println("Sum of integer array elements is " + sum);
}
}
Overriding
The process() method and int i, int j parameters in Processor are overridden in the child class MathProcessor. Line 7 and line 23:
Java
Overriding The process() method and int i, int j parameters in Processor are overridden in the child class MathProcessor. Line 7 and line 23:
public class Processor {

public void process(int i, int j) { /* ... */ }

}

/* ... */

class MathProcessor extends Processor {

@Override
public void process(int i, int j) { /* ... */ }

}
And process() method and int[] ints in Processor are also overridden in the child class. Line 11 and line 28:
public class Processor {

public void process(int[] ints) { /* ... */ }

}

/* ... */

class MathProcessor extends Processor {

@Override
public void process(Object[] objs) { /* ... */ }

}
Overloading
The process() method is overloaded in the Processor class. Lines 7, 11, and 15:
public class Processor {

public void process(int i, int j) { /* ... */ }

public void process(int[] ints) { /* ... */ }

public void process(Object[] objs) { /* ... */ }

}