Scala – Method Overriding

  • date 24th April, 2021 |
  • by Prwatech |
  • 0 Comments

In scala we can redefine the same method to do different operations but with the same method name, this concept is called Method Overriding.

scala uses override modifier to override method from super class to sub class in the same code.

use case 1:

scala> class Prwa(val name: String) {

     override def toString = getClass.getName + “[name=” + name + “]”

     }

scala> class Edu(codename: String) extends Prwa(codename) {

     override val name = “Edu”

     override val toString = “Edu”

     }

use case 2:

scala> class animal(val name: String) {

       override def toString = getClass.getName + “[name=” + name + “]”

     }

scala> class wild(codename: String) extends animal(codename) {

     override val name = “wild”

     override val toString = “wild”

     }

use case 3:

scala> class fruit(val name: String) {

       override def toString = getClass.getName + “[name=” + name + “]”

     }

scala> class Mango(codename: String) extends fruit(codename) {

     override val name = “Mango”

     override val toString = “Mango”

     }

0
0

Quick Support

image image