Scala | Polymorphism

Polymorphism is the ability of any data to be processed in more than one form. The word itself indicates the meaning as poly means many and morphism means types. Scala implements polymorphism through virtual functions, overloaded functions and overloaded operators. Polymorphism is one of the most important concept of object oriented programming language. The most common use of polymorphism in object oriented programming occurs when a parent class reference is used to refer to a child class object. Here we will see how represent any function in many types and many forms. Real life example of polymorphism, a person at the same time can have different roles to play in life. Like a woman at the same time is a mother, a wife, an employee and a daughter. So the same person has to have many features but has to implement each as per the situation and the condition. Polymorphism is considered as one of the important features of Object Oriented Programming. In Scala the function can be applied to arguments of many types, or the type can have instances of many types.
There are two principal forms of polymorphism:

  • Subtyping: In subtyping a subclass’s instance can be passed to a base class
  • Generics: By type parameterization, instances of a function or class are created .

Below are some examples:
Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to shows the usage of 
// many functions with the same name 
class example 
      
    // This is the first function with the name fun
    def func(a:Int) 
    
        println("First Execution:" + a); 
    
      
    // This is the second function with the name fun
    def func(a:Int, b:Int) 
    
        var sum = a + b; 
        println("Second Execution:" + sum); 
    
      
    // This is the first function with the name fun
    def func(a:Int, b:Int, c:Int) 
    
        var product = a * b * c; 
        println("Third Execution:" + product); 
    
  
// Creating object
object Main      
    // Main method
    def main(args: Array[String]) 
    {
        // Creating object of example class
        var ob = new example(); 
        ob.func(120); 
        ob.func(50, 70);
        ob.func(10, 5, 6);
    

chevron_right


Output:



First Execution:120
Second Execution:120
Third Execution:300

In above example we have three functions whose name is same (func) but parameters are different.

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to illustrate polymorphism
// concept
class example2
    // Function 1 
    def func(vehicle:String, category:String)     
    
        println("The Vehicle is:" + vehicle); 
        println("The Vehicle category is:" + category); 
    
      
    // Function 2 
    def func(name:String, Marks:Int)      
    
        println("Student Name is:" + name); 
        println("Marks obtained are:" + Marks); 
    
      
    // Function 3 
    def func(a:Int, b:Int) 
    {
        var Sum = a + b;
        println("Sum is:" + Sum)
    }
      
  
// Creating object
object Main 
    // Main method
    def main(args: Array[String]) 
    
        var A = new example2(); 
        A.func("swift", "hatchback"); 
        A.func("honda-city", "sedan"); 
        A.func("Ashok", 95);
        A.func(10, 20);
    

chevron_right


Output:

The Vehicle is:swift
The Vehicle category is:hatchback
The Vehicle is:honda-city
The Vehicle category is:sedan
Student Name is:Ashok
Marks obtained are:95
Sum is:30


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.