java - error in attribute extended from trait in scala -
i using scala , have trait attribute "name" extended trait in userclass accessing attribute
when tried declare name in trait this
val name : string it gave error in child class child class ha unimplemented member when tried
val name : string = "" it worked fine please tell me difference , reason why not working before , why worked after modification
i'm assuming code looks this:
trait hasname { val name : string } class person extends hasname a trait is, nature, abstract, means allowed have unimplemented methods. in effect, trait declaring this:
all classes extending
hasnameguaranteed haveval name : stringinstance variable. in variable dependent on actual child class
in case above, code expanded to:
trait hasname { val name : string = ??? } where ??? means particular 'function' unimplemented.
after modification:
trait hasname { val name : string } class person extends hasname {val name : string = ""} the extending class, person overrides hasname trait's implementation, or in case, lack thereof, own implementation.
alternatively, if do:
trait hasname { val name : string = "" } you adding implementation hasname, every child class will, default, have implementation.
Comments
Post a Comment