Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, just like in many other object-oriented languages, a subclass can inherit members from a superclass. If you want to call a method of a superclass from a subclass (especially when you've overridden that method in the subclass), you use the super
keyword.
Here's a simple illustration:
class Animal { def speak(): String = { "Some sound" } } class Dog extends Animal { override def speak(): String = { super.speak() + " and Woof" } } val d = new Dog() println(d.speak()) // Outputs: Some sound and Woof
In the Dog
class, we've overridden the speak
method. However, inside this overridden method, we're calling the speak
method of the superclass (Animal
) using super.speak()
.
This can be particularly useful in scenarios where you want to extend the behavior of a method in the superclass without completely replacing its functionality.
Remember:
The super
keyword refers to the superclass instance of the current object.
Using super
is not limited to methods. You can also access overridden fields of a superclass using super
.
If the method in the superclass is abstract (has no default implementation), then there's no actual method to call with super
, so using super
in that context would result in a compile-time error.
Using super keyword in Scala:
super
keyword in Scala is used to refer to the superclass or supertrait of the current class or trait.class Animal { def sound(): String = "Generic animal sound" } class Dog extends Animal { def bark(): String = "Woof!" def dogSound(): String = { val genericSound = super.sound() s"$genericSound and ${bark()}" } }
Scala super keyword examples:
super
keyword is used for various purposes, including method invocation, accessing members, and constructor invocation.class Animal { val category: String = "Generic animal" def sound(): String = "Generic animal sound" } class Dog extends Animal { override val category: String = "Canine" def dogInfo(): String = s"${super.category}: ${super.sound()}" }
Inheritance and method invocation in Scala:
super
is used to invoke the superclass's method.class Animal { def sound(): String = "Generic animal sound" } class Dog extends Animal { def dogSound(): String = s"${super.sound()} and Woof!" }
Calling superclass constructor in Scala:
super
keyword is used to call the constructor of the superclass in the subclass.class Animal(name: String) { println(s"Creating animal named $name") } class Dog(name: String) extends Animal(name) { println(s"Creating dog named $name") }
Overriding and calling parent class methods in Scala:
super
keyword is often used when overriding methods in a subclass to invoke the overridden method in the parent class.class Animal { def sound(): String = "Generic animal sound" } class Dog extends Animal { override def sound(): String = s"${super.sound()} and Woof!" }
Invoking methods from a superclass in Scala:
super
can be used to invoke methods from the superclass, providing a way to extend or modify behavior.class Animal { def sound(): String = "Generic animal sound" } class Dog extends Animal { def modifiedSound(): String = s"${super.sound()} and Woof!" }
Scala super vs. super[T] for method invocation:
super
is used for invoking methods from the immediate superclass. super[T]
is used to invoke methods from a specific trait in the hierarchy.trait A { def message: String = "Trait A" } trait B extends A { override def message: String = "Trait B" } trait C extends A { override def message: String = "Trait C" } class D extends B with C { def combinedMessage(): String = s"${super[B].message} and ${super[C].message}" }
Mixins and calling methods from supertraits in Scala:
with
, and super
is used to invoke methods from supertraits.trait A { def message: String = "Trait A" } trait B extends A { override def message: String = "Trait B" } trait C extends A { override def message: String = "Trait C" } class D extends B with C { def combinedMessage(): String = s"${super[A].message} and ${super[B].message} and ${super[C].message}" }
Accessing protected members of a superclass in Scala:
protected
keyword allows access to members within the same class, subclasses, and companion objects. super
is used to access protected members.class Animal { protected def secret(): String = "Classified information" } class Dog extends Animal { def revealSecret(): String = s"${super.secret()} revealed by Dog" }
Chaining method calls with super in Scala:
super
can be used to chain method calls, invoking methods from multiple levels in the hierarchy.trait A { def message: String = "Trait A" } trait B extends A { override def message: String = s"${super.message} and Trait B" } trait C extends A { override def message: String = s"${super.message} and Trait C" } class D extends B with C { def combinedMessage(): String = super[C].message }
Calling overridden methods in Scala:
super
can be used to call the overridden method from the superclass.class Animal { def sound(): String = "Generic animal sound" } class Dog extends Animal { override def sound(): String = s"${super.sound()} and Woof!" }
Delegation vs. inheritance for method invocation in Scala:
trait SoundDelegate { def makeSound(): String } class Animal(delegate: SoundDelegate) { def sound(): String = delegate.makeSound() } class Dog extends SoundDelegate { def makeSound(): String = "Woof!" } val dog = new Dog val animal = new Animal(dog) val result = animal.sound()
Using super in abstract classes and traits in Scala:
super
is used similarly in abstract classes and traits, allowing method invocation in the hierarchy.trait A { def message: String } trait B extends A { override def message: String = s"${super.message} and Trait B" } abstract class C extends B class D extends C { def combinedMessage(): String = super.message }