By using method overloading, several methods can have the same name but different signatures, and the signature can change depending on the number, type, or combination of input arguments. Method overloading is also known as early binding, static polymorphism, and compile-time polymorphism in the Java programming language. The child argument will always take precedence over the parent argument when a method overloads.
// Method overloading example
class Product { int Multiply(int p, int q){ return p * q; } float Multiply(float p, float q,float r){ return p * q * r; } } class Overloading { public static void main(String[] args){ Product product=new Product(); System.out.println(product.Multiply(2, 4)); System.out.println(product.Multiply(5.2f, 6.3f,2.3f)); } }
Output:
8
75.34799
Modifying the number of parameters while passing to various methods allows for method overloading.
// Method Overloading by Changing the Number of Parameters.
class Product { public void adding(int a, int b){ int add = a+b; System.out.println(add); } public void adding(int a, int b, int c){ int add = a+b +c; System.out.println(add); } } class DockerTpoint { public static void main(String[] args) { Product product = new Product(); product.adding(10,20); product.adding(10, 20, 30); } }
Output:
8
32.76
When two methods share the same name but have different Data Types of parameter , this is referred to as method overloading.
// Method Overloading by Changing Data Types of the Arguments
class Product { public void adding(int a, int b){ int add = a+b; System.out.println(add); } public void adding(float a, float b, float c){ float add = a+b+c; System.out.println(add); } } class DockerTpoint { public static void main(String[] args){ Product product = new Product(); product.adding(10,20); product.adding(10.2f, 20.1f, 30.3f); } }
Output:
30
60.6
Rearranging the parameters of two or more overloading methods is another way to implement overloading of methods.
For example, if method 1's parameters are (String name, int ID) and method 2's parameters are (int ID, String name), but both methods have the same name, then these 2 methods are deemed to be overloaded with various parameter combinations.
// Method Overloading by Changing the Order of the Parameters of Methods
class Product { public void adding(int ID, String name){ System.out.println("Id : "+ID+" Name : "+name); } public void adding(String name,int ID){ System.out.println("Id : "+ID+" Name : "+name); } } class DockerTpoint { public static void main(String[] args){ Product product = new Product(); product.adding(113,"Alok"); product.adding("Alok",123); } }
Output:
Id : 113 Name :Alok
Id : 123 Name :Alok
Note : The better error is the compile time error, not the run time error. Therefore, if you declare the same method with the same parameters in Java, a compiler error is produced.
If the exact prototype does not match the arguments, the compiler will use type conversion but to a higher type within the same family. Type conversion to the following higher family (If a long data type is not available for an int data type, let's say it will look for a float data type instead).
Small Data type get promoted to high or big data type
Byte can be promoted to short, int, long, float, or double as shown in the diagram above. Promotion from the short datatype to int, long, float, or double is possible. Char can be elevated to int, long, float, double, and other datatypes.
// Method Overloading and Type Promotion
class Product { public void adding(int ID, long fee) // here int get promoted to long { System.out.println("Id : "+ID+" fee : "+fee); } public void adding(int fee,int ID){ System.out.println("Id : "+ID+" fee : "+fee); } } class DockerTpoint { public static void main(String[] args){ Product product = new Product(); product.adding(113,1400); product.adding(1400,123); } }
Output:
Id : 1400 fee : 113
Id : 123 fee : 1400
Type promotion is not done if there are type-matching arguments in the method.
// Example of Type Promotion if matching found
class Product { public void adding(int ID, long fee){ System.out.println("Check 1: Id : "+ID+" fee : "+fee); } public void adding(int fee,int ID) // here matching found so this method will get called { System.out.println("Check 2: Id : "+ID+" fee : "+fee); } } class DockerTpoint { public static void main(String[] args){ Product product = new Product(); product.adding(113,1400); } }
Output:
Check 2: Id : 1400 fee : 113
There will be ambiguity if there are no arguments of the same type in the method and each method encourages a similar number of arguments.
// Example of Type Promotion in the case of uncertainty
class Product { public void adding(int ID, long fee){ System.out.println("Check 1: Id : "+ID+" fee : "+fee); } public void adding(long fee,int ID) // here matching found so this method will get called { System.out.println("Check 2: Id : "+ID+" fee : "+fee); } } class DockerTpoint { public static void main(String[] args){ Product product = new Product(); product.adding(113,1400); //C.T Error } }
Output:
Exception in thread "main" java.lang.Error:
Unresolved compilation problem: The method adding(int, long) is ambiguous for the type Product
Post your comment