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

  • Home
  • SEARCH
  • 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 8404859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:42:28+00:00 2026-06-09T22:42:28+00:00

Is it possible to require implementations of an interface to override Object methods? For

  • 0

Is it possible to require implementations of an interface to override Object methods?

For example, if I have an interface that will commonly be used as keys in HashMaps, I may want to require all implementations of that interface to override the hashCode() and equals() methods.

However, if I have the following interface…:

public interface MyInterface(){
  @Override
  public int hashCode();

  @Override
  public boolean equals(Object o);
}

…the following implementation compiles:

public class MyImplementation(){
}

It does not require a method to override hashCode() or equals(), as it inherits these methods from Object.

One approach could be to switch to using an abstract class instead of an interface:

public abstract class MyAbstractClass(){
  @Override
  public abstract int hashCode();

  @Override
  public abstract boolean equals(Object o);
}

Then any class that extends this abstract class is required to override hashCode() and equals():

// doesn't compile - requires implementation of hashCode() and equals()
public class MyClass extends MyAbstractClass {
}

// does compile
public class MyClass extends MyAbstractClass {
  @Override
  public int hashCode(){
    // calculate hashcode
  }

  @Override
  public boolean equals(Object o){
    // check equality
  }
}

My supposition is that the difference between these is that an abstract class is an Object, so it’s below Object in the hierarchy, so all extensions are required to meet the contract in the abstract class. Whereas, interfaces are not Objects, so don’t sit in the hierarchy, but any implementation of an interface must be an Object, at which point it inherits the Object methods, and thus satisfies the interface ‘contract’.

However, back to my original question… is there a way to require implementations of an interface to override Object methods?

  • 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-09T22:42:30+00:00Added an answer on June 9, 2026 at 10:42 pm

    There is no way to accomplish what you want using only interfaces.

    An alternative (and possibly clearer) way to do this by forcing the use of an abstract base class is as follows:

    public abstract class MyAbstractClass {
      @Override public final int hashCode() { return hashCodeImpl(); }
      @Override public final boolean equals(Object o) { return equalsImpl(o); }
    
      protected abstract int hashCodeImpl();
      protected abstract boolean equalsImpl(Object o);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently noticed that on my system it is not possible to require 'lib/file.pl'
Is it possible to have php not to require the begin/end tags ( <?php
I have a couple of classes BitMask & BitMaskLarge that both implement an interface
Background: I have three Silverlight Pages, that implement my interface: interface IPageWithData<in T> where
We have recently developed a library of classes that are used within an MVC3
I am about to build a system that will have its own engine, as
Possible Duplicate: Why does NSArray arrayWithObjects require a terminating nil? It is said that
In C++, is it possible to require a method in a base class to
Possible Duplicate: lambdas require capturing 'this' to call static member function? I want to
Is it possible to define a function and use without ()? Like require(a.php) and

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.