Updated April 14, 2023
Introduction to Scala Class
Scala Class is basically a user defined blueprint having methods and fields into a single unit from which object can be created. Class is just like a home for the various methods and global variables defined that are used within its scope. We can create an object from the class using the new keyword (by initialing it calling ) using that for calling the methods inside a class. There are fields in a class providing the class state and there are methods defined inside the class that implements the behavior of a class. With the help of Object-defined, we can use all the functionalities defined in a Class.
Class Declaration
A class in scala is defined with the Keyword Class, followed by the name of the class. Starting with opening the curly braces it contains the body of the class ending with the braces closed thereafter. The class can have some optional parameters also with any superclass extension or trait defined. The public is the default modifier used for the class.
class Class_name{
// body with method and fields.
}
Example
Class Animal
{
//Method Body
//Variable Declaration
}
Output:
Defined class Animal
How does Class Work in Scala?
After the declaration of class in scala, we have all the methods and variables that we need to use. So we need to create an object when an object of a class is created the class is known to be instantiated. Once that is done we will be able to take the values and use the method used in the class.
We can create more than one instance for a class, the new keyword is used to define objects in Scala, once that object is created we can access the methods inside the class and can implement it in our code.
Let’s see how to call the class methods with objects.
scala> class Animal{
| defAn()
| {
| println("hello")
| }
| }
defined class Animal
Here we have declared a class named with Animal with a method An inside it.
scala>val e = new Animal()
e: Animal = Animal@45a4b042
Then Here in the vale, we are making a new object which can access the methods of the CLASS.
scala>e.An
hello
And then we will just call that An method which we can call with the object e and get the functionalities of that method. So it will print the Hello Message. Since the default access modifier for the elements in a class in public but we can make it private also to restrict the access for that within the class.
So memory is allocated and a reference is returned invoking the class constructor whenever a new object is created using the new operator. We can create anonymous objects also, anonymous objects are those which doesn’t have any reference.
So constructors are basically called up in a Scala Class when an object is instantiated. Scala Constructors being of two types primary and auxiliary have the same body as that of class so anything inside the class will be the part of that Constructor. So if no constructors are defined scala will automatically take this primary constructor whenever an object is created and that the constructor is called as Default constructor.
Few Points that we need to check about this default class constructor is:-
- If values defined inside are var then we can change the value for that.
- If the values defined are val inside then it will be considered as final and the values cannot be changed.
- If there is nothing defined (val,var) it will have restricted visibility.
- We can also use the access modifier while defining the values inside so that will work accordingly as per the definition of the access modifiers.
Let us check this with an example:
scala> class Animals(var a:Int , var b : Int){
| println("hello")
| }
defined class Animals
This will define a class with primary constructor Animals and whenever the object is created the resulted value is called.
scala>val b = new Animals(2,3)
hello
b: Animals = Animals@a8e6492
Whereas on the other hand if we want to have some other constructors apart from primary are known as an auxiliary constructor. We can create as many as an auxiliary constructor in our program but the primary constructor needs to be one.
The auxiliary constructor comes up with a different signature list. The auxiliary constructor comes up with a constructor call this.
classNo_Distinct(i : Int , j:Int)
{
vark :Int = 0
println("Hello")
println(i)
println(j)
println(k)
defthis(i:Int ,j:Int,k:Int) //Auxiliary Constructor
{
this(i,j) //Primary Constructor
this.k = k
}
}
If we will call this by creating the object, the Auxiliary Constructor will call both and will do what the method has in it.
Output:
Val o = new No_Distinct(3,4,0)
So from this, we see how Scala Class is useful and helpful in the object-oriented programming model.
Conclusion
Here from the above article, we came across the Scala Class and the feature it exhibits. Having a Class is a very important concept for the Scala Object-Oriented Programming model. Here with the help of Examples, we got to know about the methods a class can have with the functionalities it adheres.
Recommended Articles
We hope that this EDUCBA information on “Scala Class” was beneficial to you. You can view EDUCBA’s recommended articles for more information.