Updated May 26, 2023
Introduction to Scala Object
Scala is an object-oriented programming language, and object comes under this concept only. Everything is an object in OOPs. Object-oriented programming languages are are based on classes and objects, and implement real-world scenarios. Scala programming language creates many objects, which can be made using a a constructor or inside any method. The object in any programming language consists of identity, behavior, and state. We will discuss them in detail in the coming section.
Syntax
The object can be created by using the new keyword in the scala.
var object_name = new Class_name();
Explanation: In the syntax above, we need to provide the object name, and the object can be created by using ‘new’ keyword followed by the class name.
var myObj = new Student();
Explanation: In the above example, suppose we want to create student objects; then, we first write a new keyword followed by class name i.e. student.
How does Object work in Scala?
Objects are used to access the class function and member variable. They consist of state, behavior, and identity. We can initialize objects by using primary constructors; let’s have a look.
1. Identity: This gives a unique name to our class object. If we do not provide objects of the same class with different names, then a compile-time error will be generated. This also makes us able to interact with different objects.
2. State: State is a property of an object, and the attributes present it. This defined the attribute of the object and what this object actually is.
3. Behavior: This represents the object’s method which defines what the object actually does.
We can take one example to understand this:
Code:
class Cat
{
var color: String = "black"
var sound: String = "meow meow"
def message()
{
println("color of the cat is : " + color);
println("what sound does a cat makes : " + sound);
println("End !");
}
}
object Main
{
// Main method
def main(args: Array[String])
{
// creating object to use method of a class.
var ani = new Cat();
ani.message();
}
}
Explanation: The class name is Cat, and we have defined one method to show its behavior. message(), This method shows the object’s behavior and what it does. Color and sound are the attributes of the class. We are calling this method from the main function by creating its object.
Object creation by using the primary constructor: if our Scala class contains only one constructor and this constructor is called the primary constructor or default constructor. If we do not provide any constructor for our class, the Scala compiler will do this for us to insert a default constructor. This primary constructor contains the same name and body as its class. So if a scale inserts a default constructor for us, we do not need to create a constructor explicitly.
A primary constructor can be parameterized or non-parameterized, depending upon us. The scala primary constructor shares the same signature as the class, meaning everything inside the class we define comes under the primary constructor only, but this does not include the method declaration and definition. If the compiler inserts a constructor for us, that would be a zero-argument constructor with no parameter, so if you want to initialize a member variable, we need to define our constructor containing the required parameter.
Code:
class Student(name: String, surname: String, age: Int)
{
def getMessage()
{
println("name of Student: " + name);
println("surname of Student : " + surname);
println("age of Student :" + age);
}
}
object Main
{
def main(args: Array[String])
{
// creating object
var s = new Student("Demo hello", "bye", 50);
s.getMessage();
}
}
Explanation: In the above example, we are creating student classes that contain their name, age, and surname as class variables which are getting assigned when the object f the class is getting prepared. So in our main method, we are initializing the object with value and calling the getMessage() method to display the information.
Points to remember when working with objects in scala:
We use the ‘new’ keyword to create an object in Scala. The name of the object should be unique. We use default or parameterized constructors to create an object.
Examples to Implement Scala Object
Below are some examples of Scala Object:
Example #1
Create a simple object and access its method.
Code:
object Main extends App{
// Your code here!
// initializing ibject and calling method
var student = new Student()
student.getInformation()
}
class Student{
def getInformation(){
println("caliing method ::: ")
println("Object created successfully.!!")
}
}
Output:
Example #2
Initializing object using primary constructor in scala.
Code:
object Main extends App{
// Your code here!
// initializing ibject and calling method
var student1 = new Student("Aman", 20, "Indore", 001)
var student2 = new Student("Amit", 30, "Indore", 002)
var student3 = new Student("Arun", 22, "Indore", 003)
var student4 = new Student("Anamika", 19, "Indore", 004)
var student5 = new Student("Aditi", 18, "Indore", 005)
student1.getStrudentInfo()
student2.getStrudentInfo()
student3.getStrudentInfo()
student4.getStrudentInfo()
student5.getStrudentInfo()
}
class Student(name: String, age: Int, address: String, Id: Int){
def getStrudentInfo(){
println("name of the student is :: " +name)
println("age of the student is :: " +age)
println("address of the student is :: " +address)
println("id of the student is :: " +Id)
}
}
Output:
Example #3
One more example of creating and initializing an object in java.
Code:
object Main extends App{
// Your code here!
// initializing ibject and calling method
var flower1 = new Flower("Rose", "Red", 001)
var flower2 = new Flower("flower 2","yellow", 002)
flower1.getFlowerInfo()
}
class Flower(name: String, color: String){
var Id: Int = 0;;
def this(name: String, color: String, Id: Int){
this(name, color)
this.Id = Id
}
def getFlowerInfo(){
println("name of the flower is :: " +name)
println("color of the flower is :: " +color)
println("id of the flower is :: " +Id)
}
}
Output:
Conclusion
Scala object has state, behavior, and identity. It is part of an object-oriented programming language like other concepts; inheritance, polymorphism, abstraction, etc. They implement the real-world entity. Object helps us to interact with one other class. The object also helps us to interact with another object, but the name should be different.
Recommended Articles
We hope that this EDUCBA information on “Scala Object” was beneficial to you. You can view EDUCBA’s recommended articles for more information.