• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is a trait?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My programming experience is mostly in Objective-C. My book seems to be saying that a trait is a block of code that a subclass, if its developer wants it to, inherits from a superclass. However, I'm confused as to why maps are really collections that inherit a trait called Map. Couldn't they make a subclass called Map?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This gives a very short description: A Tour of Scala: Traits

Are you familiar with Java interfaces? A trait is a lot like a Java interface, but there are also differences - traits can have implementations for methods (making them perhaps more like an abstract class instead of an interface).

You can use traits as mixins - to add functionality to a class by letting the class inherit from the trait. A class can inherit from multiple traits. See also: Mixin Class Composition.
 
Montana Burr
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:This gives a very short description: A Tour of Scala: Traits

Are you familiar with Java interfaces? A trait is a lot like a Java interface, but there are also differences - traits can have implementations for methods (making them perhaps more like an abstract class instead of an interface).

You can use traits as mixins - to add functionality to a class by letting the class inherit from the trait. A class can inherit from multiple traits. See also: Mixin Class Composition.



I have no idea what a Java interface is. I'm used to the concept of subclassing one parent and overriding methods that I didn't want to do anything.

My understanding is that a trait is a block of code that a subclass can choose to inherit.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose you do know the concept of classes (which ofcourse also exists in Objective-C).

A trait is like a class, except that not all of its methods have to be implemented. It can declare methods but not have an implementation. For example:

Unlike a class, you cannot directly create a new instance of a trait, because not all of its methods have an implementation. You can use a trait by creating a class which extends the trait - the class should provide the implementation for the method. To continue the example:

You can instantiate that class, just like any other class:

 
Bartender
Posts: 2407
36
Scala Python Oracle Postgres Database Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case it helps, I tend to think of a trait as a way to bolt behaviour (and attributes if necessary) onto objects, without having to alter the existing inheritance tree for that object's class. So you can have lots of different objects that all include behaviour/attributes from a given trait (or multiple traits), even though they may all be instances of different parent classes. Traits can provide implementations of the methods they specify, or leave these to be implemented elsewhere if appropriate.

Traits are often presented by comparison to Java interfaces, which is not much use if you don't use Java. But Java interfaces were a sort of halfway house attempt to do the same thing i.e. add behaviour to an existing class without changing the parent class, because Java (like Scala) does not allow multiple class inheritance. As with traits, a Java class can implement several interfaces.

A Java interface is a contract that defines the method signatures you want, but (unlike Scala traits) it does not include the implementations - you have to write these methods yourself in any class that implements that interface. If you have a commonly used interface, this means you can end up having to write essentially the same method implementations many times. Interfaces are used everywhere in Java: the idea is that you code to the interface e.g. you declare references in terms of the interface, not the specific implementation class, so that you can change the specific class that implements that interface underneath, without necessarily having to change the code that calls methods on that interface. This reduces coupling between different parts of your system and is generally regarded as a Good Thing.

Scala traits are a more flexible and powerful way to achieve the same goal of combining functionality regardless of class inheritance. For example, Scala's great Collections library is built on the flexibility of being able to mix in behaviour from various traits to provide a specific set of functionality for each Collection type (such as the various concrete implementations of the Map trait) while still sharing common features as far as possible.
 
This tiny ad is suggesting that maybe she should go play in traffic.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic