Scala | Polymorphism
Polymorphism is the ability of any data to be processed in more than one form. The word itself indicates the meaning as means many and
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:
// 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
Please Login to comment...