Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8562507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:44:40+00:00 2026-06-11T16:44:40+00:00

Considering: abstract class Base { def something() = println(Base) } trait TraitA extends Base

  • 0

Considering:

abstract class Base {
  def something() = println("Base")
}

trait TraitA extends Base {
    abstract override def something() = { super.something(); println("TraitA"); }
}

class Child extends Base {
    override def something() {
        println("Child")
    }
}

And then:

val x = new Child with TraitA
x.something()

I get:

Child
TraitA

But if I use:

class Child extends Base with TraitA {
    override def something() {
        println("Child")
    }
}

val x = new Child
x.something()

I’d get only:

Child

So what are the differentes of using a trait at construction site or declaration site? Can I have the first behavior (stackable traits) while extending/with’ing the trait in the declaration of the class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:44:42+00:00Added an answer on June 11, 2026 at 4:44 pm

    Stackable traits in Scala refers to being able to mix in multiple
    traits that work together to apply multiple modifications to a method.
    This involves invoking super.theMethod and modifying its input and/or
    output. But what is super in the context of a trait? The class (or
    trait) the trait extends from? The class the trait is being mixed
    into? It depends! All the mixed in traits and all the superclasses are
    linearized. super invokes the nearest preceding definition further up
    the chain.

    read full post

    In your case:
    1. super.something() == Child.something()
    2. super.something() == Base.something() and Child.something() overrides whole chain

    Type of mixin doesn’t matter:

    class Child2 extends Child with TraitA
    
    val x2 = new Child2
    x2.something() 
    /* 
       Child
       TraitA
    */
    
    class Child3 extends Base with TraitA
    val x3 = new Child3
    x3.something()
    /* 
       Base
       TraitA
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Considering that simple java code which would not work: public class Bar extends AbstractBar{
I've got a interface that's implemented as an abstract base class with a number
I have an abstract class called Fruit. I then have a derived class called
Considering the following sample code: // delivery strategies public abstract class DeliveryStrategy { ...
I am trying to instantiate a Java abstract class from my Groovy code. Considering
Considering this part of a Java class, private List<Object> components = new ArrayList<Object>(); private
Considering following code public class A { public static void main(String[] args) { new
Let's consider the following simplified Resource hierarchy: public abstract class Resource { static public
Considering that this prints something like [E] [F] [A] [C] [A] [B] [F] [B]
Considering the following c code: typedef struct ELE *tree_ptr struct ELE { long val;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.