class PasswordCaseClass(val password:String)
trait PasswordTrait { self:PasswordCaseClass =>
override def password = "blue"
}
val o = new PasswordCaseClass("flowers") with PasswordTrait
Is it possible to override PasswordCaseClass
's password
with what is provided in PasswordTrait
? Right now, I receive this error:
e.scala:6: error: overriding value password in class PasswordCase
Class of type String;
method password in trait PasswordTrait of type => java.lang.String needs to be a stable,
immutable value
val o = new PasswordCaseClass("flowers") with PasswordTrait
^
one error found
I would like to be able to have something like this:
class User(val password:String) {
}
trait EncryptedPassword { u:User =>
def password = SomeCriptographyLibrary.encrypt(u.password)
}
val u = new User("random_password") with EncryptedPassword
println(u.password) // see the encrypted version here
In scala, trait is a collection of abstract and non-abstract methods. You can create trait that can have all abstract methods or some abstract and some non-abstract methods. A variable that is declared either by using val or var keyword in a trait get internally implemented in the class that implements the trait.
When a subclass has the same name method as defined in the parent class, it is known as method overriding. When subclass wants to provide a specific implementation for the method defined in the parent class, it overrides method from parent class.
Unlike a class, Scala traits cannot be instantiated and have no arguments or parameters. However, you can inherit (extend) them using classes and objects.
Traits does not contain constructor parameters. Abstract class contain constructor parameters. Traits are completely interoperable only when they do not contain any implementation code. Abstract class are completely interoperable with Java code.
These are the following Scala method overriding examples, let’s see them one by one: a. Example 1 In this example, class Student extends class Person. It overrides the method greet () from class Person to its own definition using the Scala ‘override’ keyword.
A class can also override a method that’s defined in a trait. Here’s an example: class Cat extends Pet { // override 'speak' override def speak(): Unit = println("meow") def comeToMaster(): Unit = println("That's not gonna happen.")
Introduction to Scala Trait Traits in Scala are like partially implemented interfaces. It may contain abstract and non-abstract methods. It may be that all methods are abstract, but it should have at least one abstract method.
It may be that all methods are abstract, but it should have at least one abstract method. Not only are they similar to Java interfaces, Scala compiles them into those with corresponding implementation classes holding any methods implemented in the traits. You can say that using Scala trait, we can share interfaces and fields between classes.
You can override a def
with a val
, but you can't do it the other way around. A val
implies a guarantee -- that it's value is stable and immutable -- that a def
does not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With