Scala Tutorial

Basics

Control Statements

OOP Concepts

Parameterized - Type

Exceptions

Scala Annotation

Methods

String

Scala Packages

Scala Trait

Collections

Scala Options

Miscellaneous Topics

Call a method on a Super Class in Scala

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.

  1. Using super keyword in Scala:

    • Description: The super keyword in Scala is used to refer to the superclass or supertrait of the current class or trait.
    • Code Example:
      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()}"
        }
      }
      
  2. Scala super keyword examples:

    • Description: The super keyword is used for various purposes, including method invocation, accessing members, and constructor invocation.
    • Code Example:
      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()}"
      }
      
  3. Inheritance and method invocation in Scala:

    • Description: Inheritance in Scala allows a subclass to inherit methods from its superclass, and super is used to invoke the superclass's method.
    • Code Example:
      class Animal {
        def sound(): String = "Generic animal sound"
      }
      
      class Dog extends Animal {
        def dogSound(): String = s"${super.sound()} and Woof!"
      }
      
  4. Calling superclass constructor in Scala:

    • Description: The super keyword is used to call the constructor of the superclass in the subclass.
    • Code Example:
      class Animal(name: String) {
        println(s"Creating animal named $name")
      }
      
      class Dog(name: String) extends Animal(name) {
        println(s"Creating dog named $name")
      }
      
  5. Overriding and calling parent class methods in Scala:

    • Description: The super keyword is often used when overriding methods in a subclass to invoke the overridden method in the parent class.
    • Code Example:
      class Animal {
        def sound(): String = "Generic animal sound"
      }
      
      class Dog extends Animal {
        override def sound(): String = s"${super.sound()} and Woof!"
      }
      
  6. Invoking methods from a superclass in Scala:

    • Description: super can be used to invoke methods from the superclass, providing a way to extend or modify behavior.
    • Code Example:
      class Animal {
        def sound(): String = "Generic animal sound"
      }
      
      class Dog extends Animal {
        def modifiedSound(): String = s"${super.sound()} and Woof!"
      }
      
  7. Scala super vs. super[T] for method invocation:

    • Description: super is used for invoking methods from the immediate superclass. super[T] is used to invoke methods from a specific trait in the hierarchy.
    • Code Example:
      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}"
      }
      
  8. Mixins and calling methods from supertraits in Scala:

    • Description: Mixins can be combined using with, and super is used to invoke methods from supertraits.
    • Code Example:
      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}"
      }
      
  9. Accessing protected members of a superclass in Scala:

    • Description: The protected keyword allows access to members within the same class, subclasses, and companion objects. super is used to access protected members.
    • Code Example:
      class Animal {
        protected def secret(): String = "Classified information"
      }
      
      class Dog extends Animal {
        def revealSecret(): String = s"${super.secret()} revealed by Dog"
      }
      
  10. Chaining method calls with super in Scala:

    • Description: super can be used to chain method calls, invoking methods from multiple levels in the hierarchy.
    • Code Example:
      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
      }
      
  11. Calling overridden methods in Scala:

    • Description: When a method is overridden in a subclass, super can be used to call the overridden method from the superclass.
    • Code Example:
      class Animal {
        def sound(): String = "Generic animal sound"
      }
      
      class Dog extends Animal {
        override def sound(): String = s"${super.sound()} and Woof!"
      }
      
  12. Delegation vs. inheritance for method invocation in Scala:

    • Description: Delegation involves using a separate object to perform a task. It can be an alternative to inheritance when method invocation needs to be controlled.
    • Code Example:
      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()
      
  13. Using super in abstract classes and traits in Scala:

    • Description: super is used similarly in abstract classes and traits, allowing method invocation in the hierarchy.
    • Code Example:
      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
      }