Scala Method Overriding

Category : Scala | Sub Category : Scala Programs | By Runner Dev Last updated: 2020-10-08 18:28:45 Viewed : 204


Scala Method Overriding 

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the super classes method. 

Example:

Following example illustrates about Scala method overriding

Save the file as −  MethodOverride.scala 

 

 package runnerdev 

class Bank {

  def getRateOfInterest() = {

    0

  }

} 

class SBI extends Bank {

  override def getRateOfInterest() = {

    7

  }

} 

class ICICI extends Bank {

  override def getRateOfInterest() = {

    6

  }

} 

class DBS extends Bank {

  override def getRateOfInterest() = {

    9

  }

} 

object MethodOverride {

  def main(args: Array[String]) {

    var s = new SBI();

    var i = new ICICI();

    var a = new DBS();

    println("SBI Rate of Interest: " + s.getRateOfInterest());

    println("ICICI Rate of Interest: " + i.getRateOfInterest());

    println("DBS Rate of Interest: " + a.getRateOfInterest());

  }

}

 


compile and run the above example as follows 

scala> scalac MethodOverride.scala

scalascala MethodOverride   

Output:

SBI Rate of Interest: 7

ICICI Rate of Interest: 6

DBS Rate of Interest: 9

Search
Sub-Categories
Related Articles

Leave a Comment: