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

Contact: java.response.email@gmail.com
Download Telegram
output
Overriding vs Overloading
1. Overloading is about the same method having different signatures. Overriding is about the same method, and same signature but different classes connected through inheritance.
2. Overloading is an example of compiler-time polymorphism and overriding is an example of run-time polymorphism.
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.…
Java Method Overloading
We can easily understand about method of overloading by the below example:

Suppose we have to write a method to find the square of an integer number. We can write this method as follows:

public void intSquare (int number)
{
int square = number * number;
System.out.println("Method with Integer Argument Called: " + square);
}
Suppost we want to find the square of 10, then to find the square of 10, we can call this method as follows:

intSquare(5);
Now, if we want to find the Square of a double type value, then we have to create another Square () method as follows:

public void doubleSquare(double number)
{
double square = number * number;
System.out.println("Method with double Argument Called: " + square);
}
Similarly, if we want to find the square of long type value, then we have to create another method as follows:

public void longSquare(long number)
{
long square = number * number;
System.out.prntln("Method with long Argument Called: " + square);
}
If we look carefully, to find the square of a number only, according to the data type of the number, we have to take three names as follows:

intSquare()

doubleSquare()

longSquare()
If it is possible that a programmer has to take only one name and the program itself decides which method to use for which type of value, then it will be easier for the programmer to get the same. There is no need to memorise the names of more than one method for type work. In Java, we can give the above three methods the same name.

If we provide only the square () name instead of giving different names to the above three methods and write the rest of the description as follows, then Java’s Compiler does not generate any error.
public void Square (int number)
{
int square = number * number;
System.out.println("Method with integer Argument Called: " + square);
}
public void Square(double number)
{
double square = number * number;
System.out.println("Method with double Argument Called: " + square);
}
public void Square(long number)
{
long square = number * number;
System.out.println("Method with long Argument Called: " + square);
}
If we define these three methods in a class, then these methods can be called Overloaded Methods as they have the same name. Let us develop CalculateSquare Class to understand this, which is as follows:
class CalculateSquare
{
public void square()
{
System.out.println("No parameter Method Called");
}
public int square(int number)
{
int square = number * number;
System.out.println("Method with integer Argument called: " + square);
}
public float square(float number)
{
float square = number * number;
System.out.println("Method with float Argument Called: " + square);
}
public static void main(String[] args)
{
CalculateSquare obj = new CalculateSquare();
obj.square();
obj.square(5);
obj.square(2.5);
}
}
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.