Monday, 20 January 2014

A CLOSER LOOK AT METHODS AND CLASSES

METHOD OVERLOADING


If a class have multiple methods by same name but different parameters, it is known as Method Overloading.Method overloading is one of the ways through which java supports polymorphism. Method overloading can be done by changing number of arguments or by changing the data type of arguments.If two or more method have same name and same parameter list but differs in return type are not said to be overloaded method.

Advantage of method overloading:
  • Method overloading increases the readability of the program.
  • It allows you to use the same name for a group of methods that basically have the same purpose.
  • It provides an easy way to handle default parameter value.

Syntax of method overloading:

class clsName
{
      // variable declaration

     // constructor declaration

     // methods
    rtype1 mthName(cparam1)
    {
             // body of method
     }

     rtype2 mthName(cparm2)
     {
             // body of method
      }


Example for method overloading

class Sample
{
       int addition(int i, int j)
       {
               return i + j ;
        }
        
        String addition(String s1, String s2)
        {
                return s1 + s2;   
         }
        
        double addition(double d1, double d2)
        {
                return d1 + d2;   
         }
}

class AddOperation
{
      public static void main(String args[])
      {
              Sample sObj = new Sample();
   
              System.out.println(sObj.addition(1,2));
              System.out.println(sObj.addition("Hello ","World"));
              System.out.println(sObj.addition(1.5,2));
       }
}

Output:

3
Hello World
3.5

CONSTRUCTOR OVERLOADING

class can have several constructors. This feature of java is called constructor overloading. All constructor of a class must have different parameter list i.e the signature of each constructor must differ.The signature of a constructor is a combination of its name and the sequence of its parameter types.

Syntax of Overloaded Constructor:


class clsName
{
 // Variable declaration
// Constructor
 clsName1()
{
// body of constructor
}
clsName2(cparams2)
        {
// body of constructor
}
:
        :
        clsNameN(cparamsN)
        {
             // body of constructor
        }
        // Methods
}

Example for constructor overloading:

public class ConstructorDemo
{
   public ConstructorDemo()
{
      System.out.println("Inside no argument constructor");
   }
      
   public ConstructorDemo(String name)
{
      System.out.println("Inside one argument constructor in Java with name: " + name);
   }

   public static void main(String args[]) throws IOException 
{
      
     ConstructorDemo d = new ConstructorDemo(); //calling no argument constructor in java
     ConstructorDemo e = new ConstructorDemo("Testing"); //calling one argument constructor in java
  
   }
}

Output:
Inside no argument constructor
Inside one argument constructor in Java with name: Testing



No comments:

Post a Comment