The methods in scala can be parametrized with both value and types. Value parameters are enclosed within a pair of parenthesis whereas type parameters within a pair of brackets. For example;


In this example the + plus operator is used for adding numbers as well as concatenating the strings. The add method performs the addition of two numbers and returns the result. The addString method use the same + operator and concatenates the two strings specified by the user. This is default operator polymorphism provided by Scala. Below image shows the output produced by the above program.


Scala Method Polymorphism

Now let’s see an example of polymorphism in method. We will write a generic method to get the list where we will pass the type, default values and number of items in the list.


Below image shows the output produced when above program is executed in the Scala IDE.


Scala Explicitly typed References

Sometimes we need to explicitly specify the type of value “this” in a program. Let’s see an example for this scenario.

We have a Train abstract class as below.


The abstract class Train consists of List of links and compartments. The abstract class CompartmentIntf is defined and contains join method which takes Compartment as parameter. The methods compartments, links and addCompartment are declared but the implementation will be defined in concrete classes.

Now we will define the abstract class MetroTrain that extends train class as;


This class provides a partial implementation extending Train class. The implementation details are open and thus link and compartment are left abstract. We add factory methods newLink and newCompartment since it is required to create new compartment and link objects. The methods addCompartment and join are defined using factory methods.

However above class will throw compilation error in line no 11 as “type mismatch; found : CompartmentImpl.this.type (with underlying type MetroTrain1.this.CompartmentImpl) required: MetroTrain1.this.Compartment” because “this” is assigned the type CompartmentImpl, so it’s not compatible with type Compartment required by the corresponding factory method.

Let us solve this type mismatch by using explicitly typed self reference for CompartmentImpl as below.

According to the new definition of CompartmentImpl, this has type Compartment. We specify the explicit typed reference so that CompartmentImpl denotes sub type of Compartment to be instantiate.

We will now define a concrete class ConcreteMetroTrain as;


We can now instantiate CompartmentImpl because now we know that CompartmentImpl denotes a subtype of type Compartment. Now create a Scala object TestTrain as;


That’s all for polymorphic methods and explicitly typed references in scala programming, we will look into more scala features in coming posts.

By admin

Leave a Reply