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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:14:53+00:00 2026-05-16T10:14:53+00:00

I have a problem with overriding the equals method in an Enum to make

  • 0

I have a problem with overriding the equals method in an Enum to make it compatible with other classes.
The Enum implements an interface and the idea is that all implementations of this interface can be tested for equality, regardless of their type. For Example:

public interface Group {
    public Point[] getCoordinates();
}

public enum BasicGroups implements Group {
    a,b,c; // simplified, they actually have constructors
    // + fields and methods
}

public class OtherGroup implements Group {
    // fields and methods
}

If both a BasicGroup and an OtherGroup have the same coordinates (in arbitrary order) then the equals method should return true.

No problem when performing myOtherGroup.equals(BasicGroup.a) but since the equals method in Enums is final, I can’t override them.

Is there some way to work around this? Like when testing on another BasicGroup the default equals method (reference equality) is used and when testing other classes my own implementation is used. And how do I make sure that java doesn’t use the wrong one when I do BasicGroup.a.equals(myOtherGroup)?

  • 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-16T10:14:53+00:00Added an answer on May 16, 2026 at 10:14 am

    You can NOT @Override a final method (§8.4.3.3); this much is clear. enum types (§8.9) are treated very specially in Java, which is why the equals is final (also clone, hashCode, etc.) It’s simply not possible to @Override the equals method of an enum, nor would you really want to in a more typical usage scenario.

    HOWEVER, looking at the big picture, it looks like you are trying to follow the pattern recommended in Effective Java 2nd Edition, Item 34: Emulate extensible enums with interfaces (see the language guide for more information about enum):

    You have defined this interface (now documented explicitly for expected equals behavior):

    public interface Group implements Group {
        public Point[] getCoordinates();
    
        /*
         * Compares the specified object with this Group for equality. Returns true
         * if and only if the specified object is also a Group with exactly the same
         * coordinates
         */
        @Override public boolean equals(Object o);
    }
    

    It is perfectly acceptable for an interface to define how equals method for implementors should behave, of course. This is exactly the case with, e.g. List.equals. An empty LinkedList is equals to an empty ArrayList and vice versa, because that’s what the interface mandates.

    In your case, you’ve chosen to implement some Group as enum. Unfortunately you now can’t implement equals as per the specification, since it’s final and you can’t @Override it. However, since the objective is to comply to the Group type, you can use decorator pattern by having a ForwardingGroup as follows:

    public class ForwardingGroup implements Group {
       final Group delegate;
       public ForwardingGroup(Group delegate) { this.delegate = delegate; }
    
       @Override public Point[] getCoordinates() {
           return delegate.getCoordinates();
       }
       @Override public boolean equals(Object o) {
           return ....; // insert your equals logic here!
       }
    }
    

    Now, instead of using your enum constants directly as Group, you wrap them in an instance of a ForwardingGroup. Now this Group object will have the desired equals behavior, as specified by the interface.

    That is, instead of:

    // before: using enum directly, equals doesn't behave as expected
    Group g = BasicGroup.A;
    

    You now have something like:

    // after: using decorated enum constants for proper equals behavior
    Group g = new ForwardingGroup(BasicGroup.A);
    

    Additional notes

    The fact that enum BasicGroups implements Group, even though it does not itself follow the specification of Group.equals, should be very clearly documented. Users must be warned that constants must be e.g. wrapped inside a ForwardingGroup for proper equals behavior.

    Note also that you can cache instances of ForwardingGroup, one for each enum constants. This will help reduce the number of objects created. As per Effective Java 2nd Edition, Item 1: Consider static factory methods instead of constructors, you may consider having ForwardingGroup define a static getInstance(Group g) method instead of a constructor, allowing it to return cached instances.

    I’m assuming that Group is an immutable type (Effective Java 2nd Edition, Item 15: Minimize mutability), or else you probably shouldn’t implement it with enum in the first place. Given that, consider Effective Java 2nd Edition, Item 25: Prefer lists to arrays. You may choose to have getCoordinates() return a List<Point> instead of Point[]. You can use Collections.unmodifiableList (another decorator!), which will make the returned List immutable. By contrast, since arrays are mutable, you’d be forced to perform defensive copying when returning a Point[].

    See also

    • Using the decorator design pattern for a hierarchy of classes
    • when do we need Decorator Pattern?
    • com.google.common.collect.ForwardingObject
    • What is the best way to cache and reuse immutable singleton objects in Java?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem overriding the method where from...import statement is used. Some example
I am having problem with overriding Form Action URL I have a class to
I have a problem with calling an overriden method from java class. I have
I have problem with repopulating form_upload after validation. Other input fields or selectboxes are
I struck up in a Javascript method overriding problem for a while. the problem
I have a problem concerning nested templates and the overriding of the assignment operator.
I have a strange problem that probably stems from lack of understanding of how
I have an outline view delegate and am overriding outlineView:dataCellForTableColumn:item: to make the cells
This question is specifically related to overriding the equals() method for objects with a
below is the code that works fine but the only problem i have 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.