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

Output:

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

Output:

100

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

Output:

variable speed cannot override a mutable variable

Scala Field Overriding Example4

Output:

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





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA