Thursday, February 2, 2012

Method overloading in java

Introduction of Function overloading in java
In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java implements polymorphism. Java's most exciting and useful features. When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call. Thus,
overloaded methods must differ in the type and/or number of their parameters. While overloaded methods may have different return types, the return type alone is insufficient to distinguish two versions of a method. When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call. 

Overloaded Methods:
               void sum(int a, int b)
               {
                               System.out.println("Int: Sum of "+a+" and "+b+" is "+(a+b));
               }

               void sum(float a, float b)
               {
                               System.out.println("Float: Sum of "+a+" and "+b+" is "+(a+b));
               }

               void sum(String a, String b)
               {
                               System.out.println(a+" "+b);
               }
Here the method name remain unchanged but parameters are different for each method.


Method overriding
In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden.
Example of method overriding.
 class A {
              
int i, j;
             
A(int a, int b) {
                
i = a;
                
j = b;
             
}
             
void show() {
             
System.out.println("i and j: " + i + " " + j);
             
}
      
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
System.out.println("k: " + k);
}
}

class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}

  

Advantage of method overloading
According K&B, the advantage of overloading is that we provide multiple interface(not java interface) to make the life easier for programmer. say for example, you have one method process(String id) which take id to process the data. But your application is sending id as a Long data type.So you can have one more method process(Long id) .Now programmer will have multiple options, he doesn't need to convert first string into long to do the task.This way is good in applications which send same data as different data types.
Overloading is also used on constructors to create new objects given different amounts of data




No comments:

Post a Comment