Object Casting in Scala

Scala | Object Casting: In this tutorial, we are going to learn about the object casting to change the type of objects with examples.
Submitted by Shivang Yadav, on June 29, 2020

Object casting is changing the object's type from one type to another type. Instances or objects in Scala are casted using the asInstanceOf method in Scala.

asInstanceOf Method

The asInstanceOf method in Scala is used to cast the instance of one type into another type. It is a concrete method of Any class which is the root of Scala class hierarchy so it is by default inherited.

Syntax:

def asInstance[objecttype] : objecttype

The method takes an objecttype as a parameter and returns the casted object.

The syntax used for casting object using object casting using asInstanceOf method,

object.asInstanceof[type]

Program to illustrate the object casting

Program 1: Casting an object of numeric type (int) to non-numeric type (char).

object myObject {
 def flaotConvertor(a: Int): Char = {
  return a.asInstanceOf[Char]
 }

 def main(args: Array[String]) {
  val obj1 = 99
  println("Initial value is " + obj1 + ", type of value is " + obj1.getClass)
  val obj2 = flaotConvertor(obj1)
  println("Object casting Done!")
  println("Value after casting is " + obj2 + ", type of value is " + obj2.getClass)
 }
}

Output:

Initial value is 99, type of value is int
Object casting Done!
Value after casting is c, type of value is char

Explanation:

In the above code, we have created an instance of integer type and then cast it to another instance of type char. The output we got is the character converted for the integer value taking the integer value as ASCII value. For this, we have created a method named flaotConvertor() which took the object as a parameter and return the object of another type. In the function, we have used the asInstanceOf method. To find the type of object we have used the method getClass.

Program 2: Casting an object from one numeric type (int) to another numeric type (float).

object myObject {
 def flaotConvertor(a: Int): Float = {
  return a.asInstanceOf[Float]
 }

 def main(args: Array[String]) {
  val a = 10
  println("Initial value is " + a + ", type of value is " + a.getClass)
  val b = flaotConvertor(a)
  println("Object casting Done!")
  println("Value after casting is " + b + ", type of value is " + b.getClass)
 }
}

Output:

Initial value is 10, type of value is int
Object casting Done!
Value after casting is 10.0, type of value is float

Explanation:

In the above code, we have created an instance of integer type and then cast it to another instance of type float. For this, we have created a method named flaotConvertor() which took the object as a parameter and return the object of another type. In the function, we have used the asInstanceOf method. To find the type of object we have used the method getClass.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.