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.

                             

Method Overriding

Method Overriding
Method overriding :  When Subclass acquires the properties of Super class  through method then it is Method Overriding
                              Method Overriding in java is most useful features of java. Through inheritance we can reuse already existed code and through method overriding we can modify that reused code according to our requirements

Method Overriding Rules:-
  1. must be IS-A relationship (inheritance).
  2. method must have same name as in the super class
  3. method must have same parameter as in the super class.
  4. The return type of the overrided method must be compatible with super class method. If super class method has primitive data type as its return type, then overrided method must have same return type in sub class also. If super class method has derived or user defined data type as its return type, then return type of sub class method must be of same type or its sub class
  5. You cannot reduce the visibility of overloaded method in subclass.

Note- You cannot override static method in method overriding.because static method is related with class and method name with object.
dynamic/late binding polymorphism: overriding
public class Bank {
     
     
       
      int getRateOfInterest()
      {
      return 4;} 
      } 
       
      class PNB extends Bank{ 
      int getRateOfInterest(){return 9;} 
      } 
       
      class KENRA extends Bank{ 
      int getRateOfInterest(){return 6;} 
      } 
      class HDFC extends Bank{ 
      int getRateOfInterest(){return 9;} 
      } 
       
      class Test{ 
      public static void main(String args[]){ 
      PNB pnb=new PNB(); 
      KENRA kenra=new KENRA(); 
      HDFC hdfc=new HDFC(); 
      System.out.println("PNB Rate of Interest: "+pnb.getRateOfInterest()); 
      System.out.println("KENRA Rate of Interest: "+kenra.getRateOfInterest()); 
      System.out.println("HDFC Rate of Interest: "+hdfc.getRateOfInterest()); 
      } 
      }
OutPut-:
 PNB Rate of Interest: 9
KENRA Rate of Interest: 6
HDFC Rate of Interest: 9

.
.