Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
Method overriding is a feature in Scala (as well as other object-oriented languages) that allows a subclass to provide a specific implementation of a method that's already defined in its superclass or one of its superclasses. The overridden method in the subclass should have the same name, return type, and parameters as the method in the superclass.
Let's delve into method overriding in Scala:
class Animal { def sound(): String = { "Some sound" } } class Dog extends Animal { override def sound(): String = { "Woof" } } val dog = new Dog() println(dog.sound()) // Output: Woof
In this example, the Dog
class overrides the sound
method of the Animal
class.
Using the override
Keyword: In Scala, you must use the override
keyword when you override a method. This helps catch errors in case you unintentionally try to override a method that doesn't exist in a parent class.
Cannot Override a final
Method: If a method is declared as final
in the superclass, it cannot be overridden in any subclass.
Same Method Signature: The overriding method must have the same name, return type (or subtype), and same parameter list as the method of the parent class.
Super Keyword: You can use the super
keyword to refer to the overridden method or other members of the superclass.
class Cat extends Animal { override def sound(): String = { super.sound() + "... but usually Meow" } } val cat = new Cat() println(cat.sound()) // Output: Some sound... but usually Meow
class Shape(val name: String) { def description: String = s"I am a shape named $name." } class Circle(override val name: String, val radius: Double) extends Shape(name) { override def description: String = super.description + s" I am specifically a circle with a radius of $radius." } val circle = new Circle("MyCircle", 5.0) println(circle.description)
Overriding a Variable: In Scala, you can override a val
or a var
with another val
, but there are some rules and considerations to keep in mind. Typically, overriding fields is less common and can lead to more complex and hard-to-read code, so use it judiciously.
Overriding Type Members: In addition to methods and fields, Scala allows for the overriding of type members.
Method overriding in Scala enables subclasses to provide specific implementations for methods already defined in a superclass, allowing for polymorphism. It's essential to be familiar with the rules and semantics of overriding to avoid common pitfalls and to produce clear, maintainable code.
How to override methods in Scala:
class Animal { def makeSound(): String = "Generic animal sound" } class Cat extends Animal { override def makeSound(): String = "Meow!" } val cat = new Cat() val sound = cat.makeSound() // "Meow!"
Scala abstract classes and method overriding:
override
keyword.abstract class Shape { def area(): Double } class Circle(radius: Double) extends Shape { override def area(): Double = math.Pi * radius * radius }
Using the 'override' keyword in Scala:
override
keyword is used to explicitly indicate that a method in a subclass or trait is intended to override a method in a superclass or parent trait.class Dog extends Animal { override def makeSound(): String = "Woof!" }
Scala traits and method overriding:
trait Speaker { def speak(): String } class Person extends Speaker { override def speak(): String = "Hello, World!" }
Dynamic method dispatch in Scala:
val animal: Animal = new Cat() val dynamicSound = animal.makeSound() // Resolves to "Meow!" at runtime
Super keyword in Scala method overriding:
super
keyword is used to invoke the overridden method from the superclass or parent trait within the overriding method.class Subclass extends Superclass { override def overriddenMethod(): String = { val resultFromSuper = super.overriddenMethod() s"Modified result: $resultFromSuper" } }
Preventing method override in Scala:
final
in Scala, preventing them from being overridden in subclasses or traits.class NonOverridable { final def cannotOverride(): String = "This cannot be overridden" }