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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:25:10+00:00 2026-06-18T23:25:10+00:00

I want to create a solution for the following problem without much redundancy: I

  • 0

I want to create a solution for the following problem without much redundancy:

I have an abstract class Unit, subclasses SubUnit1 and SubUnit2. All Units have a maxValue, which differs from SubUnit1 to SubUnit2, but should be the same for all instances of the same sub unit at any time.

I don’t want to copy all the setters, getters or some kind of method around these values, because they are identical for all subclasses. I also don’t want to use normal paramters and hand down the methods to subclasses and update every single instance of these subclasses if necessary. And somehow get the current value when I create a new instance.

Is there a way to declare some sort of static parameter and methods in the parent class Unit, that differ in the different subclasses?

  • 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-18T23:25:11+00:00Added an answer on June 18, 2026 at 11:25 pm

    You could define a protected constructor in the parent class that accepts the value of the MAXVALUE

    public abstract class Unit {
       private final int maximum;
    
       protected Unit(int maximum) {
          this.maximum = maximum;
       }
    
       public int getMaximum() {
          return maximum;
       }
    }
    

    Then in your derived classes you expose constructors that call the parent’s constructor with a fixed value, unique per subclass.

    public class SubUnit1 {
       private static final int SUBUNIT1_MAX = 10;
    
       public SubUnit1() {
          super(SUBUNIT1_MAX);
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a Dynamic aspx Page in current solution through code-behind..forexample i
i want create image animation , i have 50 images with png format now
EDIT2 Here is the solution that works for the following problem of giving a
I'm back to C/C++ after some break. I've a following problem: I've a solution
I have the following XML file: <xml version=1.0 encoding=utf-8?> <Data> <Parameter1>1</Parameter1> </Data> I want
I have a following problem. I'm trying to implement a model for my QTreeView
I have an Excel with the following structure which I want to convert to
I have the following code to create a ViewStack which is used as a
Please could someone suggest the best possible solution to the following problem (this is
I would like to have some suggestions for the following problem: Let's say you

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.