11. Polymorphism

Friday, August 21, 2009 Posted by Sudarsan
Method Overloading

In Java Polymorphism can be achieved through method overloading and constructor overloading.

Method overloading is defined as the function that enables you to define two or more methods with the same name but with different signatures within the class.

The methods that share the same name, but have different signatures are called overloaded methods.

The signature of a method consists of:

1. The name of the method.
2. The number of arguments it takes.
3. The data type of the arguments.
4. The order of the arguments.

When an overloaded method is invoked, the Java compiler uses the type of arguments or the number of arguments to determine which copy of overloaded method to invoke.

Overloading methods must differ in the type or the number of parameters.
The return types of overloaded methods can be different.

Overloaded methods are used when you need several methods that perform closely related tasks.

Method overloading is a way to implement polymorphism in Java.

The following code snippet shows an overloaded add() method:

        public void add(int a, int b)    //Add two integers
  public void add(float a, float b)   //Add two floats
 public void add(double a, double b)    //Add two doubles 
 

Have a look at the fallowing examples to learn more about polymorphism.

Example I

// OverLoad.java

/**
 *
 * @author Sudarsan
 */
class OverLoad {

    // Here we are implementing polymorphism for different data types

    // add is overloaded method
    
    public void add(int x,int y)
    {
        System.out.println("The Sum of Two Integers : "+(x+y));
    }

    public void add(float x,float y)
    {
        System.out.println("The Sum of Two Floats : "+(x+y));
    }

    public void add(double x,double y)
    {
        System.out.println("The Sum of Two Doubles : "+(x+y));
    }

}

// Main Method to construct the OverLoad object

/**
 *
 * @author Sudarsan
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        OverLoad ob=new OverLoad();

        // Passing different type of parameters for a single method
        
        ob.add(10, 20);

        ob.add(10.25F, 20.25F);

        ob.add(23.33, 43.33);
    }

}

If we compile this program we can get the fallowing output

The Sum of Two Integers : 30
The Sum of Two Floats : 30.5
The Sum of Two Doubles : 66.66

Have a look at this example to understand polymorphism in more realistic way.

// Customer.java 

/**
 *
 * @author Sudarsan
 */
class Customer {

  public int customerID ;
  public String name;
  public String address;
  public String phoneNumber;
  public String eMailAddress; 

  // This method is used for customers who don't have email
  public void setCustInfo(int ID, String n, String a, String p)
  {
    customerID = ID;
    name = n;
    address = a;
    phoneNumber = p;
  }

  // This method is used for customers who have email
  public void setCustInfo(int ID, String n, String a, String p, String e)
  {
    customerID = ID;
    name = n;
    address = a;
    phoneNumber = p;
    eMailAddress = e;
  }

  public void display() {

    System.out.println("Customer ID: " + customerID);
    System.out.println("Name:" + name);
    System.out.println("Address: " + address);
    System.out.println("Phone: " + phoneNumber);
    System.out.println("E-Mail (optional): " + eMailAddress);
  } 

}

// Main class.java is used to construct Customer

/**
 *
 * @author Sudarsan
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here


    Customer customerOne = new Customer();
    Customer customerTwo = new Customer();

    customerOne.setCustInfo(111, "Hrithik Roshan", "100 Home Finders Delhi", "111-111");

    customerTwo.setCustInfo(222, "Sachin Tendulkar", "200 Andheri Mumbai.", "222-222","sachin@live.com");

    customerOne.display();
    customerTwo.display();
    }

}

When we compile the above program we can see the fallowing output

Customer ID: 111
Name:Hrithik Roshan
Address: 100 Home Finders Delhi
Phone: 111-111
E-Mail (optional): null
Customer ID: 222
Name:Sachin Tendulkar
Address: 200 Andheri Mumbai.
Phone: 222-222
E-Mail (optional): sachin@live.com

So, you can observe that, we have used only one method display() and setCustInfo() to differ the customers with e-mail and without e-mail.
Labels:

Post a Comment