·  JAVA – Syntax – Terms – Overloading

 

- Info:

  - Overloaded methods have the same name but different number or type of parameters.

  - Java doesn't support methods with variable number of parameters.

  - Instead you have to make separate function for different number of parameters which results in overloading.

 

- Example:

  - In this example Person constructor (method) is overloaded since two constructors have the same name but diferent

    number and type of parameters.

  - Create JAVA Console Application Test.java

 

    Test.java

    public class Test {

      

      public static void main(String[] args) {

        Person john = new Person("John");

               john.say();

               

        Person jill = new Person("Jill", 25);

               jill.say();          

      }   

      

    }

    

    class Person {

      String name;

      int    age;

      void say() { System.out.println(name + " is " + age); }

      

      //CONSTRUCTOR WITH ONE PARAMETER.

      Person(String name) {

        this.name = name;

      }

    

      //CONSTRUCTOR WITH TWO PARAMETERS.

      Person(String name, int age) {

        this.name = name;

        this.age = age;

      }

    }