Method Overloading and Constructor Overloading in Java

Method Overloading and Constructor Overloading in Java

Method Overloading in Java

Overloading allows different methods to have same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both. Overloading is related to compile time (or static) polymorphism.

// Java program to demonstrate working of method overloading in Java.

public class Sum {

    // Overloaded sum() takes two int parameters
    public int sum(int x, int y) {
        return (x + y);
    }

    // Overloaded sum() takes three int parameters
    public int sum(int x, int y, int z) {
         return (x + y + z);
    }

    // Overloaded sum() takes two double parameters
    public double sum(double x, double y) {
         return (x + y);
    }  

    
    public static void main(String args[]) {
        Sum s = new Sum();
        System.out.println(s.sum(11, 22));
        System.out.println(s.sum(11, 22, 33));
        System.out.println(s.sum(10.5, 20.5));
    }
}

What is the advantage?
We don’t have to create and remember different names for functions doing the same thing. For example, in our code, if overloading was not supported by Java, we would have to create method names like sum1, sum2,… or sum2Int, sum3Int, … etc.

Can we overload static methods?

The answer is ‘Yes’. We can have two or more static methods with same name, but differences in input parameters.

Can we overload methods that differ only by static keyword?

We cannot overload two methods in Java if they differ only by static keyword (number of parameters and types of parameters is same). See following Java program for example.
Can we overload main() in Java?

Like other static methods, we can overload main() in Java.

// A Java program with overloaded main()
import java.io.*;
  
public class Test {
      
    // Normal main()
    public static void main(String[] args) {
        System.out.println("Hi Piyush");
        Test.main("Hello");
    }
  
    // Overloaded main methods
    public static void main(String arg1) {
        System.out.println("Hi, " + arg1);
        Test.main("Dear Piyush","My Piyush");
    }
    public static void main(String arg1, String arg2) {
        System.out.println("Hi, " + arg1 + ", " + arg2);
    }
}

Constructor Overloading in Java

We can also overload constructors in java. Overloaded constructor is called based upon the parameters specified when new is executed. Sometimes there is a need of initializing an object in different ways. This can be done using constructor overloading.

// Java program to illustrate Constructor Overloading
class Box
{
    double width, height, depth;

    // constructor used when all dimensions specified
    Box(double w, double h, double d)
    {
        width = w;
        height = h;
        depth = d;
    }

    // constructor used when no dimensions specified
    Box()
    {
        width = height = depth = 0;
    }

    // constructor used when cube is created
    Box(double len)
    {
        width = height = depth = len;
    }

    // compute and return volume
    double volume()
    {
        return width * height * depth;
    }
}

public class Test
{
    public static void main(String args[])
    {
        // create boxes using the various constructors
        Box mybox1 = new Box(11, 22, 15);
        Box mybox2 = new Box();
        Box mycube = new Box(7);

        double vol;

        // get volume of first box
        vol = mybox1.volume();
        System.out.println(" Volume of mybox1 is " + vol);

        // get volume of second box
        vol = mybox2.volume();
        System.out.println(" Volume of mybox2 is " + vol);

        // get volume of cube
        vol = mycube.volume();
        System.out.println(" Volume of mycube is " + vol);
    }
}

Important points to be taken care while doing Constructor Overloading:

§  Constructor calling must be the first statement of constructor in Java.
§  If we have defined any parameterized constructor, then compiler will not create default constructor. and vice versa if we don’t define any constructor, the compiler creates the default constructor(also known as no-arg constructor) by default during compilation
§  Recursive constructor calling is invalid in java.

What is the difference between Overloading and Overriding?

§  Overloading is about same function have different signatures. Overriding is about same function, same signature but different classes connected through inheritance.
§  Overloading is an example of compiler time polymorphism and overriding is an example of run time polymorphism.


Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes