Class in Object Oriented Programming is a template consist of variables and methods that are required to define a Object. In this scala tutorial, we will discuss to define scala class with the help of examples.

Defining Scala Class

#1. Defining a Simple Scala Class

To define a class we use keyword class and create its instance using new keyword followed by the name of the class with or without parentheses.

In example 1 we have created classStudent and a Singleton objectStudent_Obj that contains the main method.Also a,b and c are 3 Instance of class. And we want to print the values of all three instances.

/*EXAMPLE 1*/

class Student;

 object Student_Obj {

 def main(args: Array[String]) {

 val a = new Student;//no parentheses

 println(“value of a “+a);

 val b =new Student();//using parentheses

 println(“value of b “+ b)

 val c = new Student;

 println(“value of c “+c);

}

}

When we compile and execute the above code , we will get output of each instance as string values. These values are JVM internal references for object.The method that generates string output is a toString method of java.lang.Object class.

D:\programs>scalac Student.scala //Compiling

D:\programs>scala Student_Obj //Run

/*Output after Compilation and Run*/

value of a Student@3cd3e762

value of b Student@3c9754d8

value of c Student@3bf7ca37

we can override the toString method to display any string.

/*EXAMPLE 2*/

class Student {

//Overriding toString Method

override def toString = {“Hello HadoopTree”}

}

object Student_Obj {

def main(args: Array[String]) {

val a = new Student;

println(“value of a “+a);

val b =new Student();

println(“value of b “+ b)

val c = new Student;

println(“value of c “+c); }

}

/* Output after Compilation and Run */

value of a Hello HadoopTree

value of b Hello HadoopTree

value of c Hello HadoopTree

A class can also contain methods and/or fields(variable), a field can be defined using  val or var . Now let us modify above Student class with some fields and method.

/*EXAMPLE 3*/

class Student {

//Fields

var name:String=”Ravi”;

var age:Int=19;

//Method

def display = {“Age of ” + name + ” is ” + age};

}

object Student_Obj {

def main(args: Array[String])

{ val s1 = new Student;

s1.name=”john”;

s1.age=20;

println(s1.display);

val s2 =new Student;

s2.name=”David”;

s2.age=21;

println(s2.display); }

 }

/*Output after Compilation and Run */

Age of john is 20

Age of David is 21

How to Define Scala Functions and Methods ? - HadoopTree
How to Define Scala Functions and Methods ? – HadoopTree

#2. Defining  a Scala Class with input parameters

We can also define a class with input parameters. These input parameters are fields of class.

/*EXAMPLE 4*/

class Student(var name:String , var age:Int)

{

def display = {“Age of ” + name + ” is” + age}

}

object Student_Obj {

def main(args: Array[String])

{

val s1 = new Student(“john”,20);

println(s1.display);

val s2 =new Student(age=21,name=”Ravi”)

println(s2.display);

}

}

/*Output after Compilation and Run*/

Age of john is20

Age of Ravi is21

In the above example name and age are fields and their values are assigned when instance is created.

val s1 = new Student(“john”,20);
val s2 =new Student(age=21,name=”Ravi”)

Defining a class with input parameters help us to create instance of class each having unique set values for fields.

Now let us analyze example 3 & 4.  Till now we have compiled and executed both codes , let’s try to decompile the class file of  example 3 & 4  to generate java code and compare the results.

/*DECOMPILING EXAMPLE 3*/

D:\programs>scalac Student.scala //Compiling

D:\programs>javap Student //Decompiling class file to generate java code

Compiled from “Student.scala”

public class Student {

public java.lang.String name();

public void name_$eq(java.lang.String);

public int age();

public void age_$eq(int);

public java.lang.String display();

public Student();//Default Constructor

}

/*DECOMPILING EXAMPLE 4*/

D:\programs>scalac Student.scala //Compiling

D:\programs>javap Student //Decomiling class file to generate java code

Compiled from “Student.scala”

public class Student {

public java.lang.String name();

public void name_$eq(java.lang.String);

public int age();

public void age_$eq(int);

public java.lang.String display();

public Student(java.lang.String, int);//Parameterised Constructor

}

Here you will find  constructor of example 3 is Default but constructor of example 4 is Parameterised. So when you define a class with input parameters then parameterised constructor is created.

#3. Defining a Scala Class with Input Parameters and Default Values

We can also default the values for each parameters similar to functions.

/*EXAMPLE 5*/

class Student(var name:String , var age:Int=19)

{

def display = {“Age of ” + name + ” is” + age}

}

object Student_Obj {

def main(args: Array[String])

{

val s1 = new Student(“john”);

println(s1.display);

val s2 =new Student(age=21,name=”Ravi”)

println(s2.display);

}

}

/*Output after Compilation and Run*/

Age of john is19

Age of Ravi is21

Conclusion

We have discussed to define class with parameters and without parameters , and type of constructors created when defining with or without parameter. We also discussed to override toString method.