Javatpoint Logo
Javatpoint Logo

Scala Field Overriding

In scala, you can override fields also but it has some rules that need to be followed. Below are some examples that illustrate how to override fields.

Scala Field Overriding Example1


Error - variable speed needs 'override' modifier

In scala, you must use either override keyword or override annotation when you are overriding methods or fields of super class. If you don't do this, compiler reports an error and stops execution of program.

Scala Field Overriding Example2



In scala, you can override only those variables which are declared by using val keyword in both classes. Below are some interesting examples which demonstrate the whole process.

Scala Field Overriding Example3


variable speed cannot override a mutable variable

Scala Field Overriding Example4


Error - variable speed needs to be a stable, immutable value
Next TopicScala Final

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA