Sunday, 1 November 2015

METHOD OVERLOADING

Method overloading:->  When two or more methods in a class have the same method names with different arguments, it is said to be method overloading. Overloading does not block inheritance from the superclass. Overloaded methods must have different method signatures. But method name remains same.
                                                  Concentrate on these points
IN JAVA, Compiler checks method signature for duplicate methods or for method overloading. method signature consist of three things,
 1) Method Name   2) Number Of Arguments   3) Types of arguments.
NOTE-: If these three things are same for any two methods in a class, then compiler gives duplicate method error.
How to Check Compiler->Complier first check Method Name, then check further:
Ø  If Method Name is not equal then there will not be Method overloading.(as already said method name must be same.)
Ø  if Method Name is same then Complier will check Number Of Arguments, If methods differs in number of arguments, then it does not check types of argument. It treats as methods are overloaded.   
                           If number of arguments are same then compiler checks types of arguments. If types of arguments are also same, then compiler will give duplicate method error. If types of arguments are not same, then compiler will treat them as methods are overloaded. 

Yaad rhe -> In Method Overloading,  Methods may have same return types or different return types. It does not effect  on method overloading. And may have same access modifiers or different access modifiers. It also does not effect method overloading.  it is clear that compiler will check only method signature for method overloading or for duplicate methods. It does not check return types, access modifiers and static or non-static.
static/early binding polymorphism: overloading

public class TFSian_Class {
     
     
           public void read()                      // I
           {
                System.out.println("All Tfsian read");
           }
           public void read(int x)                 // II
           {
                System.out.println("Ranjeet reads more than " + x + " hours in a day");
           }
           public void read(String str)           // III
           {
                System.out.println("Reshma reads " + str);
           }
           public static void main(String args[])
           {
             TFSian_Class tfs = new TFSian_Class();

                 tfs.read();              // calls I
                 tfs.read(10);                  // calls II
                 tfs.read("SDLC");        // calls III
          }
     }

OutPut-> 
All Tfsian read
Ranjeet reads more than 10 hours in a day
Reshma read SDLC




In the above TFSian_Class  class, the same read() method is declared three times and each time takes different parameters – no parameter, int parameter and string parameter. Compiler can easily differentiate which method is to be called depending upon the number of parameters and their sequence of data types.
Return type in Overloading
See the following two method declarations.
public void read()
public int read()

The above two read() methods differ in their return type but not in parameters. Compiler cannot judge which is to be called depending on the return types; judges only on the parameter list. The above statements raise a compilation error. The return type may or may not be the same in method overloading.

                             

No comments:

Post a Comment