Institute of Computer Science
  1. Courses
  2. 2017/18 spring
  3. Object-Oriented Programming (Narva College) (P2NC.01.083)
ET
Log in

Object-Oriented Programming (Narva College) 2017/18 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
Session 2

Overloading of methods

Quite often we want to create slight variations of the same method with different parameters. For example, we might have a method multiplyNumbers that allows us to find the multiplication of two numbers:

 multiplyNumbers(4,5);

and other times we want to find the square of the number:

 multiplyNumbers(4);

Some programming languages require to come up with different names for the methods (e.g. multiplyNumbers and squareNumber). Fortunately Java allows us to have more than one method with the same name as long as they have different parameters. This process is called overloading. The primary requirement for overloading: the different methods have different method signatures.

The signature of a method is the name of the method and the data types of its parameters. Example:

 myMethod( int, double, String )

Note! The modifier and the return type are not part of the signature. Either the names of the parameters matter!

Example of method multiplyNumbers overloading (pay attention to the signatures!):

public class MultiplyNumbers{

    public static int multiplyNumbers(int a, int b){
        return a*b;
    }

    public static int multiplyNumbers(int a){
        return a*a;
    }

    public static void main(String[] args){
        System.out.println(multiplyNumbers(3,5));

        System.out.println(multiplyNumbers(5));

    }
}

Self-assessment

Session 2
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment