×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Method overloading in Scala

By IncludeHelp Last updated : October 10, 2024

Scala method overloading

Method overloading is a method that is redefined in different ways under the same name. Method overloading is one of the methods used to implement polymorphism in Scala.

Implementation of method overloading in Scala

To create an overloaded method in Scala, define multiple methods with the same name and different parameter list and return type. This means defining multiple functions one with one parameter of type ‘A’ other with two parameters, etc.

Syntax

//Method 1 : 
def fun (a : data_type ) { 
  // code to be executed
}

//Method 2 :
def fun(a : data_type , b : data_type ){
  // code to be executed
}

Both these methods contribute to method overloading in Scala. Now, to create an overloaded method can invert either of the following things in the function that lead to overloading the method:

  1. Methods with different parameter lists
  2. Methods with different data types and order

Example 1

Program to show implementation of method overloading in Scala | print area of different figures using method overloading

object MyClass {
    def peri(x:Int, y:Int){
        println("The perimeter of rectangle is "+ (x+y))   
    }
    
    def peri(a:Int , b:Int ,c:Int){
        println("The perimeter of rectangle is "+ (a+b+c))   
    }
    
    def peri(r:Int){
        println("The perimeter of rectangle is "+ (2*(3.14)*r))   
    }
    
    def main(args: Array[String]) {        
        println("Program to print perimeter of different figures using method overloading: ")
        println("Perimeter of rectangle: ")
        peri(12 , 345)
        println("Perimeter of triangle: ")
        peri(3, 5, 8)
        println("Perimeter of circle:")
        peri(4)
    }
}

Output

Program to print perimeter of different figures using method overloading: 
Perimeter of rectangle: 
The perimeter of rectangle is 357
Perimeter of triangle: 
The perimeter of rectangle is 16
Perimeter of circle:
The perimeter of rectangle is 25.12

Explanation

The above code is used to print demonstrate method overloading concept. This example is to print the perimeter of different figures using object overloading. The function peri() is overloaded and each of the different figure areas will have a different overloaded method that will be able to calculate the given area of the specific figure. The code prints the calculated value for the figure.

Example 2

Program to show implementation of method overloading in Scala | print data-type of the parameter using method overloading

object MyClass {
    def datatype(x:Int){
        println("The parameter is of Integer datatype")    
    }
    
    def datatype(x:Float){
        println("The parameter is of Float data type")    
    } 
    
    def datatype(x:Char){
        println("The parameter is of Character data type")    
    }
    
    def datatype(x: Boolean){
        println("The parameter is of Boolean data type")    
    }
    
    def main(args: Array[String]) {
        println("Program to print data type using method overloading: ")
        datatype(4)
        datatype(4.0f)
        datatype('f')
        datatype(true)
    }
}

Output

Program to print data type using method overloading: 
The parameter is of Integer datatype
The parameter is of Float data type
The parameter is of Character data type
The parameter is of Boolean data type

Explanation

The above code is used to print the data type of the variable. The method datatype() is overloaded to print the data type based on the input variable. The method can check for Int, Float, Char, Bool data type and will check for the data type, and prints the data type accordingly.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.