Object Casting in Scala Last Updated : 03 Jun, 2022 Comments Improve Suggest changes Like Article Like Report In order to cast an Object (i.e, instance) from one type to another type, it is obligatory to use asInstanceOf method. This method is defined in Class Any which is the root of the scala class hierarchy (like Object class in Java). The asInstanceOf method belongs to concrete value members of Class Any which is utilized to cast the receiver Object. Applications of asInstanceof methodThis perspective is required in manifesting beans from an application context file.It is also used to cast numeric types.It can even be applied in complex codes like communicating with Java and sending it an array of Object instances.Examples of casting using asInstanceof methodCasting from Integer to Float. Example : Scala // Scala program of Object Casting // Int to Float case class Casting(a:Int) // Creating object object GfG { // Main method def main(args:Array[String]) { val a = 10 // Casting value of a into float val b = a.asInstanceOf[Float] println("The value of a after" + " casting into float is " + b) } } Output: The value of a after casting into float is 10.0 Here, type of 'a' is Integer and after casting type will be Float, it is the example of Casting numeric types.Casting from Character to Integer. Example : Scala // Scala program of Object Casting // Char to Int case class Casting(c:Char) // Creating object object GfG { // Main method def main(args:Array[String]) { val c = 'c' // Casting value of c into Int val d = c.asInstanceOf[Int] println("The value of c after" + " casting into Integer is " + d) } } Output: The value of c after casting into Integer is 99Here, type of 'c' is character and after casting type will be integer, which gives ASCII value of 'c'.Casting from Float to Integer. Example : Scala // Scala program of Object Casting // Float to Int case class Casting(a:Float, b:Float) // Creating object object GfG { // Main method def main(args:Array[String]) { val a = 20.5 val b = 10.2 // Casting value of c into Int val c = (a/b).asInstanceOf[Int] println("The value of division after" + " casting float into Integer will be " + c) } } Output: The value of division after casting float into Integer will be 2Here, type of 'a' and 'b' is float but after Casting type will be float. Comment More infoAdvertise with us Next Article Object Casting in Scala N nidhi1352singh Follow Improve Article Tags : Scala Scala Scala-OOPS Scala-Data Type Similar Reads Class and Object in Scala Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real-life entities. Class A class is a user-defined blueprint or prototype from which objects are created. Or in other words, a class combines the fields and methods(member function which defines actions) 5 min read How to Create a JSON Object in Scala? JSON(JavaScript Object Notation) plays an important role in web development and data transmission. In web applications, we frequently use JSON, a lightweight data transmission standard. Working with JSON data is made easy using Scala, a sophisticated programming language for the Java Virtual Machine 2 min read How to mock object in Scala? In software development, mocking is a technique used to isolate the unit under test by replacing external dependencies with simulated objects or "mocks". Mocking is particularly useful when working with objects that are difficult to set up or have side effects, such as databases, web services, or fi 3 min read Determine the class of a Scala object To determine the class of a Scala object we use getClass method. This method returns the Class details which is the parent Class of the instance. Below is the example to determine class of a Scala object. Calling method with argument - Example #1: Scala // Scala program to determine the class of a S 2 min read Scala | Case Class and Case Object Explanation of Case Class A Case Class is just like a regular class, which has a feature for modeling unchangeable data. It is also constructive in pattern matching. It has been defined with a modifier case, due to this case keyword, we can get some benefits to stop oneself from doing a sections of 5 min read Like