Home Java Master-java Method Overloading
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Method Overloading

Advertisement
As we know that a method have its own signature which is known by method's name and the parameter types.

Method Overloading

     

As we know that a method have its own signature which is known by method's name and the parameter types. Java has a powerful feature which is known as method overloading. With the help of this feature we can define two methods of same name with different parameters. It allows the facility to define that how we perform the same action on different type of parameter.
Here is an example of the method overloading. Create a class "mol" and subclass "Test". Now we create two methods of same name "square" but the parameter are different. When the main class call the method the method executes on behalf of the parameter. In the given example we are trying to illustrate the same technique.

Here is the Code of the Example :

"mol.java"

import java.lang.*;

public class mol{
  public static void main(
  String args[]){
  new Test().doOverLoad();
  }
  }
  class Test{
  public void doOverLoad(){
  int x = 7;
  double y = 11.4;
  System.out.println(square(x) + "\n"+ square(y));
  }
  public int square(int y){
  return y*y;
  }
  public double square(double y){
  return y*y;
  }
  }

Here is the Output of the Example :

C:\roseindia>javac mol.java

C:\roseindia>java mol
49
129.96

Download This Example :

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 17, 2009

Ask Questions?    Discuss: Method Overloading  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com