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 8939933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:55:37+00:00 2026-06-15T10:55:37+00:00

Give me some help, I just cannot solve this problem. How do I ensure

  • 0

Give me some help, I just cannot solve this problem.
How do I ensure that constraints on getter methods are always respected?
My classes have this form:

abstract class Element {
  var name: String
  var description: String
}

class Component (var name: String, var description: String) extends Element

For the purpose of my project I need to use var.
I want to put constraints on the field name that must always be respected.
Both when I create an instance of Component as follows:

val C1 = new Component ("C1 Component", "Description of C1")

Both when I change the value:

C1.name = "new value"

If I create the classes in this way:

abstract class Element {
  protected var _name : String
  // Getter
  final def name = _name
  // Setter
  final def name_= (value:String):Unit =
    if (value.size < 5) println ("ERROR: Bad Value")
    else _name = value

  var description: String
}

class Component (protected var _name : String, var description: String) extends Element

In the main method, because I do not have control here:

val C1 = new Component ("C1", "Description of C1")

But only later, when I change the value:

C1.name = "comp"

I want my getter to always be respected.
How do I do this?

  • 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-15T10:55:38+00:00Added an answer on June 15, 2026 at 10:55 am
    class Component (_name : String, var description: String) extends Element {
      this.name = _name
    }
    

    Also, make var _name private, so subclasses can’t set it without using the setter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could anyone give me some help with this please? The content needs to appear
I have a problem that I just cannot seem to get my head around,
I've been give some lovely Java code that has a lot of things like
Anybody please give some useful links on this topic.i need to create a content
Could do with some help with this question.I am using the system.io, and I
Guys i need some help here , im getting this error here at the
I'm facing a problem I cannot solve. And I do not know whether it
Hello everybody i'm looking for some help with the next problem: i have a
To give some context, a little while ago with the help from VolkerK on
I am having some difficulty compiling a C++ program that I've written. This program

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.