Back to articles
Method Overloading in Java

Method Overloading in Java

via Dev.toHarini

Method Overloading is a feature in Java where multiple methods have the same name but different parameters. It helps improve code readability and allows you to use the same method name for different tasks. Rules for Method Overloading To overload a method, you must change at least one of the following: Number of parameters Type of parameters Order of parameters Changing only the return type is NOT allowed Example 1: Different Number of Parameters class Calci { public static void main ( String [] args ) { Calci obj = new Calci (); System . out . println ( obj . add ( 10 , 20 )); System . out . println ( obj . add ( 10 , 20 , 30 )); } int add ( int a , int b ) { System . out . println ( "2 arguments" ); return a + b ; } int add ( int a , int b , int c ) { System . out . println ( "2 arguments" ); return a + b + c ; } } Output Example 2: Different Data Types class Student { public static void main ( String [] args ) { Student obj = new Student (); obj . show ( 100 ); obj . show ( "Harini"

Continue reading on Dev.to

Opens in a new tab

Read Full Article
2 views

Related Articles