Overriding toString method in Scala

Hello everyone, In this post, We will show you, How to override toString method in Scala programming language. The sample program has been tested and posted in the same post.

Employee Class (Emploee.scala)

The below Employee class, which is overriding the toString() method and returning string value. Which contain attributes names and values.

Note: When you override the any super class method in scala. We should use override keyword before the method (ie: override def toString())

package com.dineshkrish.scala
class Employee(empId: Int, empName: String) {
def getEmpId() : Int = {
return empId;
def getEmpName() : String = {
return empName;
override def toString() : String = {
return "[empId : "+empId+", empName = "+empName+"]";

Main Class (Test.scala)

package com.dineshkrish.scala
object Test {
def main(args: Array[String]): Unit = {
var employee = new Employee(101, "Dinesh Krishnan");


[empId : 101, empName = Dinesh Krishnan]


1. Scala Language
2. Scala Programming Language


No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *