Polymorphism is the ability of any data to be processed in more than one form. The word itself indicates the meaning as
- 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:
// 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 );
}
} |
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:
// Scala program to illustrate polymorphism // concept class example 2
{ // 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 example 2 ();
A.func("swift", "hatchback");
A.func("honda-city", "sedan");
A.func("Ashok", 95 );
A.func( 10 , 20 );
}
} |
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
In Scala, polymorphism refers to the ability of a function, method or class to take on multiple forms or behaviors based on the types of input arguments or data it receives. There are two types of polymorphism in Scala:
Sure! Here’s an example of runtime polymorphism or method overriding in Scala:
class Shape {
def area() : Double = 0.0
} class Circle(radius : Double) extends Shape {
override def area() : Double = math.Pi * radius * radius
} class Rectangle(length : Double, width : Double) extends Shape {
override def area() : Double = length * width
} object Main {
def main(args : Array[String]) {
val circle = new Circle( 2.0 )
val rectangle = new Rectangle( 3.0 , 4.0 )
println(s "Area of circle: ${circle.area()}" )
println(s "Area of rectangle: ${rectangle.area()}" )
}
} |
Output
Area of circle: 12.566370614359172 Area of rectangle: 12.0
In this code, we have defined three classes Shape, Circle, and Rectangle. Shape is the base class which has a method area() that returns the area of the shape. Circle and Rectangle are the derived classes which inherit from the Shape class and override the area() method to calculate the area of a circle and a rectangle respectively.
In the Main object, we create an instance of Circle and Rectangle classes and call their area() method. Since the area() method is overridden in the derived classes, the correct implementation is used for each object, and the output shows the calculated areas of the circle and rectangle.
Sure! Here are some advantages and disadvantages of polymorphism in Scala:
Advantages:
- Code reusability: Polymorphism allows for code reusability, as methods can be defined in a base class and then overridden in derived classes, reducing code duplication.
- Flexibility: Polymorphism provides flexibility, as it allows you to write more generic code that can be applied to a wider range of objects, instead of writing separate code for each object.
- Extensibility: Polymorphism makes it easier to extend your code, as you can simply create new classes that inherit from existing classes and override or add methods as necessary.
Disadvantages:
- Runtime performance overhead: Polymorphism can introduce some runtime performance overhead, as the method to be called is determined at runtime, which requires additional time to resolve the method call.
- Complexity: Polymorphism can make code more complex and harder to understand, especially when multiple levels of inheritance are involved.
- Name clashes: Polymorphism can lead to name clashes, as multiple classes can define methods with the same name and parameters. This can lead to confusion and errors if the wrong method is called.
- Overall, the advantages of polymorphism generally outweigh the disadvantages, as it provides greater code reusability, flexibility, and extensibility, which are important principles of good software design.