Java Methods Method Overloading

Java Methods Method Overloading

Using Methods

The procedures and functions are known as methods in Java. The methods scope is for the whole class. Now here, you can make a small difference. A function in C-lang can be called without object but a method is to be called with an object. The first line of a method declaration is known as "method signature". A Java method signature may comprises of an access specifier, one or two optional access modifiers, return type, name, optional parameter list and also optional exceptions the method can throw. The parameter or a return type can be a variable, an object or an array. Following program illustrates.

public class Employee
{
  public void nature()
  {
    System.out.println("Hard working");
  }
  public int total(int basic, int da, int hra)
  {
    int sum = basic + da + hra;
    return sum;
  }                      
  public static void main(String args[])
  {
    Employee emp1 = new Employee();
    emp1.nature();
    int salary = emp1.total(8000, 2000, 1000);
    System.out.println("The total salary Rs." + salary);
  }
}

Java Methods Method Overloading

Output screen of Employee.java

The above code involves two methods – nature() and total(). The nature() method just prints a message and does not have parameters and return type. But total() method takes three parameters and a return type. The return type must be only one value for a method. You will not see any new information here, it is same as C/C++. Observe, the methods are called with object emp1 (like instance variables). To make the program simple, instance variables are not used. We will write another program later with instance variables and methods. The global variables in Java are known as "instance variables".

To talk technically, a method gives behavior to an object. For example, an Employee object emp1 is given nature and total salary though methods nature() and total(). Later even to change the nature of emp1, the same methods are to be used. Methods are useful to write the logic and to give actions to an object by manipulating the properties of an object.

Similar explanation is available at Three Great Principles – Data Binding, Data Hiding, Encapsulation.
Method Overloading

Java Methods Method Overloading

Observe the following functions of C-lang.

void display() { }
void display(int x) { }

The above two functions of the same name display() raises compilation error in C-lang as C cannot differentiate the same functions depending on their parameters. But C++ and Java can differentiate the same named functions depending on their parameters. The same method declared, in the same class, a number of times, but with different parameters is known as "method overloading". With method overloading, the same method called at different times gives different outputs (functionalities).

public class Animal
{
  public void eat()				// I
  {
    System.out.println("All animals eat");
  }
  public void eat(int x)			// II
  {
    System.out.println("Whale eats more than " + x + " tons of food a day");
  }
  public void eat(String str)		        // III
  {
    System.out.println("Parrots eat " + str);
  }
  public static void main(String args[])
  {
    Animal a1 = new Animal();
    a1.eat(); 			// calls I
    a1.eat(5); 			// calls II
    a1.eat("grains"); 		// calls III
  }
}

Java Methods Method Overloading

Output screen of Animal.java of Java Methods Method Overloading

In the above Animal class, the same eat() 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 (of Java Methods Method Overloading)

See the following two method declarations.

public void eat()
public int eat()

The above two eat() 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.

Static Binding

Compiler decides at compile time itself which overloaded method is to be called. That is, method binding is done at compile time itself. This is known as "static binding". At runtime, simply methods are called. Java also supports dynamic binding which leads to dynamic polymorphism.

Static Polymorphism

In Greek, "poly" means "many" and "morphism" means "forms". Polymorphism means many forms of the same method – the same method, called at different times, gives different types of output. The three eat() methods print different functionalities of the nature of animals like whale and parrot. Because the polymorphism is achieved statically, at compile time, it is known as "static polymorphism". Java achieves static polymorphism with method overloading.

1. Can we Overload main?

9 thoughts on “Java Methods Method Overloading”

  1. Sir as we know name mangling is used in c and there is namemangler to perform mangling…

    But i want to knw whether java also uses name mangling or not..

    And if not wht is used by java for differentiating between overloaded method…also whether it is necessary for java or not..??

  2. class Calculation
    {
    void sum(int a,double b)
    {
    System.out.println(a+b);
    }
    void sum( double a, int b)
    {
    System.out.println(a+b);
    }

    public static void main(String args[])
    {
    Calculation obj=new Calculation();
    obj.sum(10,10);
    obj.sum(20,20);

    }
    }
    getting error as:Calculation.java:15: reference to sum is ambiguous, both method sum(int,double)
    in Calculation and method sum(double,int) in Calculation match
    obj.sum(10,10);
    ^
    Calculation.java:16: reference to sum is ambiguous, both method sum(int,double)
    in Calculation and method sum(double,int) in Calculation match
    obj.sum(20,20);
    bt when change prgm as:
    class Calculation
    {
    void sum(int a,double b)
    {
    System.out.println(a+b);
    }
    void sum( double a, int b)
    {
    System.out.println(a+b);
    }

    public static void main(String args[])
    {
    Calculation obj=new Calculation();
    obj.sum(10,(double)10);
    obj.sum((double)20,20);

    }
    }
    its complng success and getting output. Can anyone explain what is happening here.

    1. void sum(int a,double b) —- A
      void sum( double a, int b) —- B

      obj.sum(10,10);

      Think sharp. 10, 10 can go to A or B. The first parameter 10 can be int in A or can be converted to double in B. The same case with second parameter 10 also. This is the ambiguity. Ambiguity in implicit casting.

      In the second example, you are converting explicitly and there by no ambiguity to compiler.

  3. Hi sir this web site is pretty well can i have all this data in one file if possible please provide me that file.

Leave a Comment

Your email address will not be published.