
Method OverLoading
1. What is Method Overloading? Method Overloading is a feature in Java where multiple methods have the same method name and same number of parameters,arguments but different parameters , different data type in the same class. It is the one of the Polymorphism called the Compile-Time Polymorphism The methods differ by: Number of parameters Type of parameters Order of parameters Example public class Sample { public static void main ( String [] args ){ Sample casio = new Sample (); casio . add ( 10 , 20 ); casio . add ( 10 , 20 , 30 ); casio . add (); } public void add ( int no1 , int no2 ){ System . out . println ( "2 arguments" ); } public void add (){ System . out . println ( "0 arguments method" ); } public void add ( int no1 , int no2 , int no3 ){ System . out . println ( "3 arguments" ); } } Output 2.Rules of Method Overloading Not Change the Number of Parameters class SumExample { int sum ( int a , int b ) { return a + b ; } int sum ( int a , int b , int c ) { return a + b + c ; }
Continue reading on Dev.to
Opens in a new tab
