Bright Java Tutorial

is brought to you by DoITBright LLC

Java

Overloading

Overloading is an Object Oriented Programming Concept that allows allows a class to provide different implementations of a method inside the same class using the exact same name. To accomplish this, there are few rules to follow. Take a look at the example code provided below.

  package com.example.core.oop3;

  /**
   * This demonstrates the rules of overloading a method.
   * Overloading Rule : To overload a method, the method 
   * signature should be different than the method we are 
   * trying to overload. Method signature is made of 
   * number of arguments, type of arguments and order of 
   * arguments. Overloading happens in methods in the 
   * same class. It does not have anything to do with 
   * Parent and Child relationship.
   * 
   * @author Rolan Liwanag
   *
   */
  public class OverloadingDemo {

    public void aMethod() {
      System.out.println("A method in same class named " 
        + "aMethod with no arguments.");
    }

    /*
     * This is an overloading method of aMethod because it 
     * accepts an int parameter.
     */
    public void aMethod(int anInt) {
      System.out.println("A method in same class named " 
        + "aMethod with int argument.");
    }
	
    /*
     * This is an overloading method of aMethod because it 
     * accepts a String parameter.
     */
    public void aMethod(String aString) {
      System.out.println("A method in same class named " 
        + "aMethod with String argument.");
    }

    /*
     * This is an overloading method of aMethod because it 
     * accepts an int and a String parameter.
     */
    public void aMethod(int anInt, String aString) {
      System.out.println("A method in same class named " 
        + "aMethod with an int and a String argument.");
    }
	
    /*
     * This is an overloading method of aMethod because it 
     * accepts an int and a String parameter. However, 
     * the order of the parameters are different than the 
     * other overloading method in this class.
     */
    public void aMethod(String aString, int anInt) {
      System.out.println("A method in same class named " 
        + "aMethod with a String argument and an int.");
    }
	
	/*
	 * This is not overloading and it will produce 
	 * compilation error. The reason is, return type 
	 * is not part of method signature.
	 */
    /*public long aMethod() {
      System.out.println("A method in same class named " 
        + "aMethod with no arguments.");
    }*/

    /*
     * This is not overloading and it will produce 
     * compilation error. The reason is, Exception thrown 
     * is not part of method signature.
     */
    /*public void aMethod() throws Exception {
      System.out.println("A method in same class named " 
        + "aMethod with no arguments.");
    }*/

    /*
     * Access modifier is not part of method signature. 
     * This will cause compilation error.
     */
    /*private void aMethod() {
      System.out.println("A method in same class named " 
        + "aMethod with no arguments.");
    }*/
	
    public static void main(String[] args) {
      OverloadingDemo demo = new OverloadingDemo();
      demo.aMethod();
      demo.aMethod(1);
      demo.aMethod("abc");
      demo.aMethod(1, "abc");
      demo.aMethod("abc", 1);
    }
  }

                            



Back    Next