Overloading Methods in Java

Methods overloading is where a class has methods having the same name but different parameters. Importance of method overloading The m...

Methods overloading is where a class has methods having the same name but different parameters.

Importance of method overloading

The main reason to use method overloading is to increase program readability.Think of a scenario where you have addition method taking different parameters.eg   add(int a ,int b)  and addMethod(int a, int b,int c).Reading such code can be difficult.With method overloading we can use the same method name to include the two methods in the class i.e add(int a,int b) and add(int a,int b,int c)


Two ways of overloading.

There are two ways of overloading methods.
1.Changing Number of arguments


public class AddMethods {
    
    public void addNumbers(int a,int b)
    {
        int sum=a+b;
        System.out.println("Sum is " +sum);
    
    }
    public void addNumbers(int a,int b,int c)
    {
        int sum=a+b+c;
        System.out.println("Sum is " +sum);
    
    }
    
  AddMethods()
  {
    addNumbers(10,14);
      addNumbers(10,14,56);
  }
  public static void main(String args[])
  {
  new AddMethods();
  }
    
}

Output 



2.Changing Data types

public class AddMethods {
    
    public void addNumbers(int a,int b)
    {
        int sum=a+b;
        System.out.println("Sum is " +sum);
    
    }
    public void addNumbers(double a, double b)
    {
        double sum=a+b;
        System.out.println("Sum is " +sum);
    
    }
    
  AddMethods()
  {
    addNumbers(10,14);
      addNumbers(10.57,14.67);
  }
  public static void main(String args[])
  {
  new AddMethods();
  }
    
}


Output

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Overloading Methods in Java
Overloading Methods in Java
https://2.bp.blogspot.com/-a2kL2UWU36s/Wd57xcln-pI/AAAAAAAAJLE/WPA5GHcsRkozvtQFPBDlhl0c8EOhjEl9wCEwYBhgL/s1600/overloading-in-java.PNG
https://2.bp.blogspot.com/-a2kL2UWU36s/Wd57xcln-pI/AAAAAAAAJLE/WPA5GHcsRkozvtQFPBDlhl0c8EOhjEl9wCEwYBhgL/s72-c/overloading-in-java.PNG
Java Codes
http://www.download-all.net/2017/10/overloading-methods-in-java.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/10/overloading-methods-in-java.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy