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

Contact: java.response.email@gmail.com
Download Telegram
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) { /* ... */ }

}
Conclusion
In this article, we covered overriding and overloading in Java. Overriding occurs when the method signature is the same in the superclass and the child class. Overloading occurs when two or more methods in the same class have the same name but different parameters.
Java pinned «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.…»
Instruction

Explore essential Java topics that assess comprehension of management concepts for building a solid foundation. Browse through the sorted topics and click to access the corresponding posts.
Java pinned «Instruction Explore essential Java topics that assess comprehension of management concepts for building a solid foundation. Browse through the sorted topics and click to access the corresponding posts.»