Skip to main content

Method Overloading in Java Best Example For Biggeners and Students.

  1. class plus{  

  2. static int add(int a,int b)
  3. {
  4.     return a+b;
  5. }  

  6.    static int add(int a,int b,int c) 
  7.    {
  8.       return a+b+c;
  9.    }  
  10. }  

  1. class Overloading1
  2. {  
  3. public static void main(String[] args)
  4.    {  

  5.       //access the add method with 2 param from class plus,
  6.      //pass 2 arguments and print the return value
  7.      System.out.println(plus.add(10,10));  

  8.       //access the add method with 3 param from class plus, 
  9.       //pass  3 arguments and print the return value
  10.       System.out.println(plus.add(10,10,10));  
  11.     }
  12. }  

It will produce the following results:

20
30

Comments

Popular posts from this blog

Array in java Array is a variable that can hold more than one value at a time. Declaration :    int arr[] = {elements}    or    int [] arr = {elements} Intialization :    int arr[] = {2, 4, 6, 7,}     Example :    public class arrLesson {                public static void main(String           args[]) {                // declares an array                 int arr[] = { 2, 3, 2, 9, 3 };                                 for ( int i=0; i < arr. length ;       i++)      {                      /* outputs each element                     ...
Java if else statement     If else statement is a conditional statement that executes a block of code once the condition is true otherwise  executes else block if the condition becomes false.    Syntax :             if (condition) {                   //  code to execute              } else {                      //  code to execute                   }     Example :         public class conditionalProg {                    public static void     main(String args[]) {                    /* declaration and                   ...