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

The Archive Base Latest Questions

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

I want to create an abstract Parameter class. public abstract class Parameter{ } I

  • 0

I want to create an abstract Parameter class.

public abstract class Parameter{

}

I will have at least two subclasses:

public class ParamDouble extends Parameter{
    public final double MIN;
    public final double MAX;
    private double value;

    public ParamDouble(double min, double max, double current){
         this.MIN = min; 
         this.MAX = max; 
         this.value = current; 
         }



    public void setValue(double v) {
        this.value = v;
        }
    public double getValue(){
        return this.value;
        }

}

and:

public class ParamInt extends Parameter{
    public final int MIN;
    public final int MAX;
    private int value;

    public ParamDouble(int min, int max, int current){
         this.MIN = min; 
         this.MAX = max; 
         this.value = current; 
         }



    public void setValue(int v) {
        this.value = v;
        }
    public int getValue(){
        return this.value;
        }

}

So all subclasses will require, the finals MIN and MAX, and value, and contain three argument constructors, and setValue() and getValue(), but they have different types.

How can I declare these variables in the abstract 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-13T23:18:48+00:00Added an answer on June 13, 2026 at 11:18 pm

    You only need a single generic class with the correct type bound:

    public class Parameter<T extends Number & Comparable<T>> {
        public final T min;
        public final T max;
        private T value;
    
        public Parameter(T min, T max, T current) {
            this.min = min; 
            this.max = max; 
            setValue(current); // Use setter to check range on construction 
        }
    
        public void setValue(T v) {
            // being Comparable, we can check the range generically
            if (max.compareTo(v) < 0) { 
                throw new IllegalArgumentException("Value exceeds " + max);
            }
            if (min.compareTo(v) > 0) {
                throw new IllegalArgumentException("Value is below " + min);
            }
    
            this.value = v;
        }
    
        public T getValue() {
            return this.value;
        }
    }
    

    Explanation of <T extends Number & Comparable<T>>

    The bound of T is Number & Comparable<T>, which means it must be both a Number and a Comparable<T>. This is done because Number does not implement Comparable, which has the compareTo() method, but all the java.lang.Number classes (eg Integer) do, and you need the compareTo() method to check parameter range in the setValue() method.

    Without this special bound, you can’t check the range generically.

    The other major change was to make min and max instance variables, rather than static ones. You might consider having some static min/max values to pass into the constructor, or to implement subclasses like this:

    public class IntegerParameter extends Parameter<Integer> {
    
         public IntegerParameter(Integer current) {
             // specify a default min/max for the Integer impl
             super(-1, 999, current); 
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an abstract class 'A' and class 'B' and 'C' extends A. I
I have an abstract class that has two constructors. When another class inherits this
I want to create an abstract base class for all paramter-type classes to inherit
I have a class declared as such: public abstract class CrmAttribute <T> : ICrmAttribute
I have abstract class: public abstract class MyClass { public abstract string nazwa {
For simplicity's sake, lets say I have the following Abstract Base Controller Class: public
If I have following type hierarchy: abstract class TicketBase { public DateTime PublishedDate {
I have the following setup, abridged for brevity: public abstract class AuditableEntity { public
i want create image animation , i have 50 images with png format now
I want to create a new field (or two) in my table that is

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.