Scala Method Overriding

Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2020-10-08 12:58:45 Viewed : 503


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

In Scala, method overriding is a fundamental feature of object-oriented programming that allows a subclass to provide a specific implementation of a method that is already defined in its superclass. This allows you to create specialized behavior for a subclass while maintaining a common interface defined in the superclass. Method overriding is a key concept in achieving polymorphism and code reuse in object-oriented programming.

Here is an overview of method overriding in Scala:

  1. Inheritance: To perform method overriding, you need a superclass (also called a base class) and a subclass (also called a derived class) that extends the superclass.

  2. Signature Matching: The method in the subclass must have the same name, return type, and parameter types as the method in the superclass. This is known as a matching method signature.

  3. override Keyword: In Scala, you use the override keyword when defining a method in a subclass to explicitly indicate that you intend to override a method from the superclass.

  4. Access Control: The access level of the overriding method in the subclass cannot be more restrictive than the access level of the overridden method in the superclass. It can be equally or less restrictive but not more restrictive.

Here is an example of method overriding in Scala:

scala
class Animal { def speak(): Unit = { println("Animal speaks") } } class Dog extends Animal { // Method overriding override def speak(): Unit = { println("Dog barks") } } object MethodOverridingExample { def main(args: Array[String]): Unit = { val animal: Animal = new Dog() animal.speak() // Calls the speak method of the Dog class } }

In this example:

  • We have a Animal superclass with a speak method that prints "Animal speaks."

  • We have a Dog subclass that extends Animal and overrides the speak method to print "Dog barks."

  • In the MethodOverridingExample, we create an instance of Dog but assign it to a reference of type Animal. When we call animal.speak(), it invokes the overridden speak method in the Dog class, demonstrating polymorphism and dynamic method dispatch.

Output:

Dog barks

Method overriding allows you to provide specialized behavior in subclasses while maintaining a common interface, promoting code reuse and flexibility in your object-oriented designs.

Search
Related Articles

Leave a Comment: