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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:36:30+00:00 2026-06-12T05:36:30+00:00

i have different classes representing differents units (volume, weight, distance), with an enum with

  • 0

i have different classes representing differents units (volume, weight, distance), with an enum with a value for storing the conversion values to a base type.

The problem is that there is a lot of code duplication and
i’m sure there is an elegant way to write an abstract class that would avoid it.
I don’t know how i should declare this class though.
Here is the volume class:

import java.math.BigDecimal;

import lombok.Data;

@Data
public class Volume {

public enum Unit
{
    CUBIC_METER(new BigDecimal("1")), // CUBIC_METER
    // 1 m3 = 61023.7 inch3
    CUBIC_INCH(new BigDecimal("61023.7"));
    private Unit(BigDecimal m3Value)
    {
        this.m3Value = m3Value;
    }

    public final BigDecimal m3Value;
}
// internally, we store the volume in m3
private final   BigDecimal  volumeInM3;

public Volume()
{
    volumeInM3 = BigDecimal.ZERO;
}

public Volume(final String volumeValue, final Unit volumeUnit)
{
    this(new BigDecimal(volumeValue), volumeUnit);
}

public Volume(final BigDecimal volumeValue, final Unit volumeUnit)
{
    if (volumeValue.signum() == 0)
    {
        volumeInM3 = BigDecimal.ZERO;
    }
    else
    {
        volumeInM3 = volumeValue.divide(volumeUnit.m3Value, NumberUtil.MC);
    }
}

/**
 * Return the volume in the unit given in param
 * @param volumeUnit
 * @return
 */
public BigDecimal getAs(final Unit volumeUnit)
{
    return volumeInM3.multiply(volumeUnit.m3Value, NumberUtil.MC);
}

public Volume add(final Volume volumeToAdd)
{
    BigDecimal newVolumeValue = volumeToAdd.volumeInM3.add(volumeInM3, NumberUtil.MC);
    return new Volume(newVolumeValue, Volume.Unit.CUBIC_METER);
}

public Volume divide(final Volume divisor)
{
    BigDecimal newVolumeValue = volumeInM3.divide(divisor.volumeInM3, NumberUtil.MC);
    return new Volume(newVolumeValue, Volume.Unit.CUBIC_METER);
}

public boolean isZero()
{
    if (volumeInM3.signum() == 0)
        return true;
    return false;
}

public boolean isEqual(final Volume another)
{
    if (volumeInM3.compareTo(another.volumeInM3) == 0)
        return true;
    return false;
}
}

And here is the quite similar Weight class:

import java.math.BigDecimal;

import lombok.Data;

@Data
public class Weight {

// the value stored with enum is the value used to convert the unit to kilogramm, 
// wich is the reference unit
public enum Unit
{
    KILOGRAM(new BigDecimal("1")),
    // 1 kg = 1000 g 
    GRAM(new BigDecimal("1000")),
    // 1 kg = 2.20462 pounds
    POUND(new BigDecimal("2.20462"));

    private Unit(BigDecimal kgValue)
    {
        this.kgValue = kgValue;
    }

    private final BigDecimal kgValue;
}

// internally, we store the weight inKg
private final BigDecimal weightInKg;

public Weight()
{
    weightInKg = BigDecimal.ZERO;
}

public Weight(final String weightValue, final Unit weightUnit)
{
    this(new BigDecimal(weightValue), weightUnit);
}

public Weight(final BigDecimal weightValue, final Unit weightUnit)
{
    if (weightValue.signum() == 0)
    {
        weightInKg = BigDecimal.ZERO;
    }
    else
    {
        weightInKg = weightValue.divide(weightUnit.kgValue, NumberUtil.MC);
    }
}

/**
 * Return the weight in the unit given in param
 * @param weightUnit
 * @return
 */
public BigDecimal getAs(final Unit weightUnit)
{
    return weightInKg.multiply(weightUnit.kgValue, NumberUtil.MC);
}

public Weight add(final Weight weightToAdd)
{
    BigDecimal newWeightValue = weightToAdd.weightInKg.add(weightInKg, NumberUtil.MC);
    return new Weight(newWeightValue, Weight.Unit.KILOGRAM);
}

public Weight divide(final Weight divisor)
{
    BigDecimal newWeightValue = weightInKg.divide(divisor.weightInKg, NumberUtil.MC);
    return new Weight(newWeightValue, Weight.Unit.KILOGRAM);
}
public boolean isZero()
{
    if (weightInKg.signum() == 0)
        return true;
    return false;
}

public boolean isEqual(final Weight another)
{
    if (weightInKg.compareTo(another.weightInKg) == 0)
        return true;
    return false;
}
}

When instanciating a volume, the user is forced to provide explicitely the unit.

Volume myCubicMeter, myCubicInch;
myCubicMeter = new Volume("1", Volume.Unit.CUBIC_METER);
myCubicInch = new Volume("1", Volume.Unit.CUBIC_INCH);

What i’m trying to achieve is an abstract class that would implement all the methods and force the subclasses to implements the enumeration with values.
What would be the correct way to do that?

  • 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-12T05:36:32+00:00Added an answer on June 12, 2026 at 5:36 am

    In Java, enumerations are classes that are subclasses of the provided Enum class.

    You cannot give them a common abstract ancestor, neither you can subclass them. If you really want to keep the commodity of referring to your units with compile time constants from a set of an enumeration then you can’t do it.

    If you discard using enumerations and maybe declare them as normal classes then you can do it freely as with everything else, eg

    class VolumeUnit extends AbstractUnit {
      public static final VolumeUnit CUBIC_METER = new VolumeUnit(params);
      ..
    
      VolumeUnit(..) {
        ..
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have different values in different classes. I need to insert them in the
I have many different small classes which have a few fields each, e.g. this:
I have a few different classes which origin is a another class. I have
I have a need for two slightly different classes, that have the same members,
I have a page/gsp that displays 3 different classes. This means that I need
I have 2 different group page with 2 look alike classes which using the
I have a header menu and try to define different CSS classes for each
I have classes that are named exactly the same across different plug-ins that I
I have two classes that represent two different database entities. Their relationship is 1:m
I have multiple classes that will store similar data for different uses and I

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.