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

  • Home
  • SEARCH
  • 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 3391344
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:47:36+00:00 2026-05-18T03:47:36+00:00

Suppose I would like to extend class C , getting SubC . Now I

  • 0

Suppose I would like to extend class C, getting SubC.

Now I would like to access the variable c in SubC as presented in the example below in the method printC.

The following does not work, as calling printC on a SubC instance will print SubC‘s c and not C‘s c (I guess I could have picked better names…)

class C(protected var c : Int) {
  def update(n : Int) {
    c = n
  }
}

class SubC(c: Int) extends C(c) {
  def printC : Unit = {
    println(c)
  }
}

object Main {
  def main(args: Array[String]) {
    val subC = new SubC(1)
    subC.update(3)
    subC.printC // prints 1
  }
}

A possible (but undesirable) solution would be:

class SubC(cc: Int) extends C(cc) {
  def printC = {
    println(c)
  }
}

This works but it introduces a new (and unneeded) identifier cc into scope.

Is there a better (cleaner) way of doing this?

PS: To put the example above into some context. What I actually want to do is to augment C with some traits without introducing new identifiers into scope.

  • 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-05-18T03:47:37+00:00Added an answer on May 18, 2026 at 3:47 am

    This is probably the best way to augment C with traits without introducing new identifiers in scope:

    class C(protected var c : Int) {
      def update(n : Int) {
        c = n
      }
    }
    
    trait CanPrintC {
      self: C =>
      def printC : Unit = {
          println(c)
      }
    }
    class SubC(c: Int) extends C(c) with CanPrintC 
    
    object Main {
      def main(args: Array[String]) {
        val subC = new SubC(1)
        subC.update(3)
        subC.printC // prints 3
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a particular UTC time T . I would like a method
Suppose I would like to count characters in some mFile like this: while((c =
suppose I have a long algorithm which I would like to be optionally verbose
I would like to count the occurrences of a character in a string, suppose
I would like to invoke a simple method (no arguments, returns void) from within
I would like to concatenate a string within a ternary operator in EL(Expression Language).
I would like to run some command (e.g. command ) using perl's system() .
i would like to publish/produce a message with some message id, like each message
I'm having an issue sending array parameters to a Struts 2 action class. I
Suppose I develop an application with the logic as a CLI program that can

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.