Scala – Polymorphism

  • date 8th October, 2020 |
  • by Prwatech |
  • 0 Comments

Polymorphism is the capability of any data to be processed in more than one form. Scala executes polymorphism through virtual functions, over-loaded functions and over-loaded operators. Polymorphism is perhaps the main ideas of object oriented programming language. The most widely recognized utilization of polymorphism in object oriented programming happens when a parent class reference is utilized to refer to a child class object.

poly means many and morphism means types

There are two forms of polymorphism:

Sub typing: In subtyping a subclass’s instance can be passed to a base class

Generics: By using type parameterization, instances of a function or class are created .

Use case 1:

class prwatech {
// This is the first function with the name fun
def func(a:Int)
{
     println("Result:" + a);
}
// This is the second function with the name fun
    def func(a:Int, b:Int) 
{ 
    var sum = a + b; 
    println("Result of a + b is:"  + sum); 
} 

// This is the first function with the name fun
   def func(a:Int, b:Int, c:Int) 
{ 
    var product = a * b * c; 
    println("Result of a * b * c is:"  + product); 
} 
}
// Creating object
object Main
{
// Main method
    def main(args: Array[String])
   {
   // Creating object of class
   var ob = new prwatech();
   ob.func(10);
   ob.func(8, 8);
   ob.func(1, 3, 6);
   }
}
output:
Result:10
Result of a + b is:16
Result of a * b * c is:18

Use case 2:

class prwatech {
// This is the first function with the name fun
// Function 1
def func(Company:String, Domain:String)
{
println("Company is:" + Company: String);
println("Domain is:" + Domain: String);
}
// Function 2 
def func(name:String, Salary:Int)      
{ 
    println("Employee Name is:" + name); 
    println("Salary is:" + Salary); 
} 

// 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 prwatech();
A.func("ABC", "Data Science");
A.func("xyz","BigData");
A.func("Michael", 100000 );
A.func(10, 20);
}
}

Output:
Domain is:Data Science
Company is:xyz
Domain is:BigData
Employee Name is:Michael
Salary is:100000
Sum is:30

Use case 3:

class eduprwa {
// This is the first function with the name fun
def func(a:Int)
{
     println("Result:" + a);
}
// This is the second function with the name fun
    def func(a:Int, b:Int) 
{ 
    var diff = a - b; 
    println("Result of a - b is:"  + diff); 
} 

// This is the first function with the name fun
   def func(a:Int, b:Int, c:Int, d:Float) 
{ 
    var expression = a * b * c + d  ; 
    println("Result of a * b / c + d is:"  + expression); 
} 
}
// Creating object
object edu
{
// Main method
    def main(args: Array[String])
   {
   // Creating object of class
   var ob = new eduprwa();
   ob.func(10);
   ob.func(8, 8);
   ob.func(1, 3, 6, 5);
   }
}

Result:10
Result of a - b is:0
Result of a * b / c + d is:23.0

Quick Support

image image