41

I'm not sure what the purpose of override keyword is, in scala. If I have

trait Shape { def foo(v: Int) }
class Triangle extends Shape { override def foo(v: Int) {} }

it behaves (apparently at least) exactly the same as it does without override.

3 Answers 3

75

In the case you are implementing an abstract method as in your example, it is not strictly necessary to add the override modifier.

However, in case you want to override a concrete method from the superclass, the override modifier is necessary. This is to avoid accidental overrides which could happen with mixing composition -- mixing-in traits during some refactoring could easily introduce a method definition that could be overridden by the method defined in the body of the class, hence the need for explicitly stating that a method is an override.

35

In your particular case, you got a comprehensive answer from axel22. I just want to add, that there is at least one more case where you may encounter override modifier. The keyword can also be used with trait methods.

Imagine that you have an abstract class:

abstract class Writer {
  def print(str: String)
}

and its concrete implementation that prints on a console

class ConsoleWriter extends Writer {
  def print(str: String) = println(str)
}

Now, you want to create a trait that will modify its behaviour. Look at the following implementation:

trait Uppercase extends Writer {
  abstract override def print(str: String) = 
    super.print(str.toUpperCase())
}

Notice that a method has two modifiers: abstract and override. This is only allowed for traits and it means that the trait must be mixed into some class that has a concrete definition of the method in question

With the definition above, you can do:

val writer = new ConsoleWriter with Uppercase
writer.print("abc")

which will yield the result

ABC

Much in the same vain, you can add more traits:

trait WithSpaces extends Writer {
  abstract override def print(str: String) = 
    super.print(str.split("").mkString(" ").tail)
}

Now when you call

val writer = new ConsoleWriter with Uppercase with WithSpaces
writer.print("abc")

you will see:

A B C

The above usage of an override modifier in traits is a distinguishing feature in scala and you won't see it in java.

1
  • Fantastic explanation of Scala traits! Commented Mar 19, 2018 at 11:32
11

It's for error checking.
Suppose you have

trait Shape { def foo(v: Int) = 1 }
class Triangle extends Shape { override def foo(v: Int) = 2 }

and then you change Shape to

trait Shape { def bar(v: Int) = 1 }

In that case the "override" will tell you that the foo in Triangle overrides nothing.

See also:
http://docs.oracle.com/javase/7/docs/api/java/lang/Override.html
http://en.wikipedia.org/wiki/C%2B%2B11#Explicit_overrides_and_final

7
  • That's only correct if the method that was overriden was abstract, see axel22 s answer. Also, both of your links are irrelevant because they apply to Java and C++, not Scala in particular.
    – Cubic
    Commented Apr 19, 2013 at 17:40
  • 2
    Cubic, it does not matter whether the overriden method is abstract, this can change with time. The interface change, the override helps to stay in tune.
    – ArtemGr
    Commented Apr 19, 2013 at 17:43
  • Your answer implies that the override keyword behaves the same as the @Override annotation in Java. That's only correct when overriding abstract methods.
    – Cubic
    Commented Apr 19, 2013 at 17:45
  • 2
    Cubic, trait Shape { def bar(v: Int) }; class Triangle extends Shape { override def foo(v: Int) = 2 } gives an error "method foo overrides nothing", it has nothing to do with whether the bar is abstract or not.
    – ArtemGr
    Commented Apr 19, 2013 at 17:49
  • For example, you could implement a method bar in Triangle and then decide to add abstract method bar to Shape and then remove abstract method foo. The foo in Triangle will then "override nothing", even though the foo in Shape was abstract.
    – ArtemGr
    Commented Apr 19, 2013 at 17:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.