Scala Classes Objects
π
2026-06-19 | π Scala
A class is an abstraction of an object, and an object is a concrete instance of a class. A class is abstract and does not occupy memory, while an object is concrete and occupies storage space. A class is a blueprint for creating objects; it is a software template that defines the methods and variables included in objects of a specific type.\\n\\nWe can use the new keyword to create an object of a class, as shown in the following example:\\n\\n## Instance\\n\\nclass Point(xc: Int, yc: Int){\\n\\nvar x: Int = xc\\n\\nvar y: Int = yc\\n\\ndef move(dx: Int, dy: Int){\\n\\n x = x + dx\\n\\n y = y + dy\\n\\n println ("x coordinate point: " + x);\\n\\n println ("y coordinate point: " + y);\\n\\n}\\n\\n}\\n\\nClasses in Scala are not declared as public. A Scala source file can contain multiple classes.\\n\\nThe class in the above example defines two variables **x** and **y**, and a method: **move**, which has no return value.\\n\\nScala class definitions can have parameters, called class parameters, such as xc and yc above. Class parameters can be accessed throughout the class.\\n\\nNext, we can use new to instantiate the class and access the methods and variables in the class:\\n\\n## Instance\\n\\nimport java.io._\\n\\nclass Point(xc: Int, yc: Int){\\n\\nvar x: Int = xc\\n\\nvar y: Int = yc\\n\\ndef move(dx: Int, dy: Int){\\n\\n x = x + dx\\n\\n y = y + dy\\n\\n println ("x coordinate point: " + x);\\n\\n println ("y coordinate point: " + y);\\n\\n}\\n\\n}\\n\\nobject Test {\\n\\ndef main(args: Array){\\n\\nval pt =new Point(10, 20);\\n\\n// Move to a new location\\n\\n pt.move(10, 10);\\n\\n}\\n\\n}\\n\\nExecuting the above code, the output result is:\\n\\n$ scalac Test.scala $ scala Test x coordinate point: 20 y coordinate point: 30\\n\\n* * *\\n\\n## Scala Inheritance\\n\\nInheriting a base class in Scala is very similar to Java, but we need to note the following points:\\n\\n* 1. You must use the override modifier when overriding a non-abstract method.\\n* 2. Only the primary constructor can pass parameters to the base class constructor.\\n* 3. You do not need to use the override keyword when overriding an abstract method of a superclass in a subclass.\\n\\nLet's look at an example:\\n\\n## Instance\\n\\nclass Point(xc: Int, yc: Int){\\n\\nvar x: Int = xc\\n\\nvar y: Int = yc\\n\\ndef move(dx: Int, dy: Int){\\n\\n x = x + dx\\n\\n y = y + dy\\n\\n println ("x coordinate point: " + x);\\n\\n println ("y coordinate point: " + y);\\n\\n}\\n\\n}\\n\\nclass Location(override val xc: Int, override val yc: Int,\\n\\nval zc :Int)extends Point(xc, yc){\\n\\nvar z: Int = zc\\n\\ndef move(dx: Int, dy: Int, dz: Int){\\n\\n x = x + dx\\n\\n y = y + dy\\n\\n z = z + dz\\n\\n println ("x coordinate point : " + x);\\n\\n println ("y coordinate point : " + y);\\n\\n println ("z coordinate point : " + z);\\n\\n}\\n\\n}\\n\\nScala uses the extends keyword to inherit a class. In the example, the Location class inherits the Point class. Point is called the parent class (base class), and Location is called the subclass.\\n\\n**override val xc** overrides the field of the parent class.\\n\\nInheritance inherits all the properties and methods of the parent class. Scala only allows inheriting from one parent class.\\n\\nThe example is as follows:\\n\\n## Instance\\n\\nimport java.io._\\n\\nclass Point(val xc: Int, val yc: Int){\\n\\nvar x: Int = xc\\n\\nvar y: Int = yc\\n\\ndef move(dx: Int, dy: Int){\\n\\n x = x + dx\\n\\n y = y + dy\\n\\n println ("x coordinate point : " + x);\\n\\n println ("y coordinate point : " + y);\\n\\n}\\n\\n}\\n\\nclass Location(override val xc: Int, override val yc: Int,\\n\\nval zc :Int)extends Point(xc, yc){\\n\\nvar z: Int = zc\\n\\ndef move(dx: Int, dy: Int, dz: Int){\\n\\n x = x + dx\\n\\n y = y + dy\\n\\n z = z + dz\\n\\n println ("x coordinate point : " + x);\\n\\n println ("y coordinate point : " + y);\\n\\n println ("z coordinate point : " + z);\\n\\n}\\n\\n}\\n\\nobject Test {\\n\\ndef main(args: Array){\\n\\nval loc =new Location(10, 20, 15);\\n\\n// Move to a new location\\n\\n loc.move(10, 10, 5);\\n\\n}\\n\\n}\\n\\nExecuting the above code, the output result is:\\n\\n$ scalac Test.scala $ scala Test x coordinate point : 20 y coordinate point : 30 z coordinate point : 20\\nTo override a non-abstract method in Scala, you must use the override modifier.\\n\\n## Instance\\n\\nclass Person {\\n\\nvar name =""\\n\\noverride def toString = getClass.getName + "[name=" + name + "]"\\n\\n}\\n\\nclass Employee extends Person {\\n\\nvar salary =0.0\\n\\noverride def toString =super.toString + "[salary=" + salary + "]"\\n\\n}\\n\\nobject Test extends App {\\n\\nval fred =new Employee\\n\\n fred.name="Fred"\\n\\n fred.salary=50000\\n\\n println(fred)\\n\\n}\\n\\nExecuting the above code, the output result is:\\n\\n$ scalac Test.scala $ scala TestEmployee[salary=50000.0]\\n\\n* * *\\n\\n## Scala Singleton Objects\\n\\nIn Scala, there is no static keyword, but it also provides a way to implement the singleton pattern, which is using the object keyword.\\n\\nWhen using the singleton pattern in Scala, in addition to the defined class, you must also define an object with the same name. The difference between it and a class is that an object cannot take parameters.\\n\\nWhen a singleton object shares the same name with a class, it is called the companion object of that class. You must define the class and its companion object in the same source file. The class is called the companion class of this singleton object. A class and its companion object can access each other's private members.\\n\\n### Singleton Object Instance\\n\\n## Instance\\n\\nimport java.io._\\n\\nclass Point(val xc: Int, val yc: Int){\\n\\nvar x: Int = xc\\n\\nvar y: Int = yc\\n\\ndef move(dx: Int, dy: Int){\\n\\n x = x + dx\\n\\n y = y + dy\\n\\n}\\n\\n}\\n\\nobject Test {\\n\\ndef main(args: Array){\\n\\nval point =new Point(10, 20)\\n\\n printPoint\\n\\ndef printPoint{\\n\\nprintln ("x coordinate point : " + point.x);\\n\\nprintln ("y coordinate point : " + point.y);\\n\\n}\\n\\n}\\n\\n}\\n\\nExecuting the above code, the output result is:\\n\\n$ scalac Test.scala $ scala Test x coordinate point : 10 y coordinate point : 20\\n### Companion Object Instance\\n\\n## Instance\\n\\n/* File name: Marker.scala\\n\\n* author:\\n\\n* url:example.com\\n\\n*/\\n\\n// private constructor\\n\\nclass Marker private(val color:String){\\n\\nprintln("create" + this)\\n\\noverride def toString(): String ="color tag:"+ color\\n\\n}\\n\\n// Companion object, with the same name as the class, can access the class's private properties and methods\\n\\nobject Marker{\\n\\nprivate val markers: Map[String, Marker]= Map(\\n\\n"red" ->new Marker("red"),\\n\\n"blue" ->new Marker("blue"),\\n\\n"green" ->new Marker("green")\\n\\n)\\n\\ndef apply(color:String)={\\n\\nif(markers.contains(color)) markers(color)else null\\n\\n}\\n\\ndef getMarker(color:String)={\\n\\nif(markers.contains(color)) markers(color)else null\\n\\n}\\n\\ndef main(args: Array){\\n\\n println(Marker("red"))\\n\\n// Singleton function call, omitted.(Dot)Symbol \\n\\n println(Marker getMarker "blue")\\n\\n}\\n\\n}\\n\\nExecuting the above code, the output result is:\\n\\n$ scalac Marker.scala $ scala Markercreatecolor tag:red createcolor tag:blue createcolor tag:green color tag:red color tag:blue