Polymorphic Types – Scala Tutorial
Polymorphism is the capability of any data to be process 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 orient 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.Polymorphic Types – Scala Tutorial
poly means many and morphism means types
There are two forms of polymorphism:
Sub typing: In subtyping a subclass’s instance can be passe to a base class
Generics: By using type parameterization, instances of a function or class are create .
Polymorphic types in Scala refer to the ability to write generic, reusable code that can operate on values of different types while maintaining type safety. Scala supports two primary forms of polymorphism: parametric polymorphism (generics) and subtype polymorphism (inheritance and subtyping).
Parametric polymorphism allows developers to define functions and data structures that can operate on values of any type. This is achieved using type parameters, denoted within square brackets ([T]
), which represent placeholder types that are specified when the function or data structure is used.
. Scala supports subtype polymorphism using traits and abstract classes, allowing for the implementation of polymorphic behavior through method overriding and dynamic dispatch.
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
2Use 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