Showing posts with label Method Overloading. Show all posts
Showing posts with label Method Overloading. Show all posts

Jul 5, 2020

Method Overloading in JAVA | Java Language | Coding Winds

METHOD OVERLOADING

The concept method overloading comes into play when we create different number of methods having same method name but different parameter list. When we call a method in java, first the name of the method matches and they it checks what kind of parameter-list is passed into the method. The method is then called based upon the types or number of parameters being allowed in, while writing the methods and the number of them actually passed by the user. So this process is also known as polymorphism, which is one of the pillars of java. So let's see an example to understand it’s implementation.

Ex:

import java.util.*;

class Solution {

 void sum(int a, int b) {

              int c= a+b;

          System.out.println(c);

         }

 void sum(double a, double b) {

      double c=a+b;

      System.out.println(c);

 }

 void sum(float a , float b) {

      float c= a+b;

      System.out.println(c);

 }

}

 

public class Coding_Winds {

  public static void main(String[] args) {

           Scanner sc = new Scanner(System.in);

 Solution obj = new Solution();

           obj.sum(2,4);

           obj.sum(2.9, 3.33);

           obj.sum(2.1f,9.7f);

       }

}

 

In the above example we have three methods having same name but different parameter limitations, so while calling the methods the compiler will check the parameter allowed with parameter passed( while calling the methods), and then call the method accordingly.

OUTPUT

6

6.23

11.799999

We can also overload a constructor since it is quite similar to a method, and also it is quite required to optimize the code using the constructors for passing different values parameters list for one methods.

Ex:

import java.util.*;

class Halloween{

     int length,breadth;

     Halloween(int a, int b){

         length=a;

         breadth=b;

     }

     Halloween(int a){

         length=a;

         breadth=a;

     }

     Halloween(){

         length=0;

         breadth=10;

     }

 void sum() {

              int c= length+breadth;

          System.out.println(c);

         }

 }

 

public class HackerRank {

     public static void main(String[] args) {

           Scanner sc = new Scanner(System.in);

 Halloween obj = new Halloween(2,3);

 Halloween obj2 = new Halloween(1);

 Halloween obj3 = new Halloween ();

 obj.sum();

 obj2.sum();

 obj3.sum();

     }

}

 

OUTPUT

5

2

10

 

So if we use the constructors in our code then we will be able to write the method body only once ,hence reducing the time.

 

Hope you are clear on this  topic do read our more articles on JAVA LANGUAGE.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


This article is SUBMITTED By : Pranjal Rai


Do subscribe to our daily blog update by clicking here.


Thank You!