How can I make an interface instance method accept arguments of the same class only, really?

Marcus Junius Brutus

This SO discussion proposes the following idiom:

public interface IComparable<T extends IComparable<T>> {
    int compare(T t);
}

which then allows:

public class Foo implements IComparable<Foo> {
    public int compare(Foo foo) {
        return 0;
    }
}

However, this idiom allows more than just the above as the following code compiles as well:

class A implements IComparable<A> {
    public int compare(A a) {return 0;}
}

public class Foo implements IComparable<A> {

    public int compare(A a) {
        return 0;
    }
}

Therefore (unless I've misunderstood something) the original idiom doesn't really buy anything more compared to the far less dramatic:

public interface IComparesWith<T> {
    int compare(T t);
}

public class A implements IComparesWith<A> {
    public int compare(A a) {...}
}

So, is there a way to actually declare an interface such that whatever class declares to implement it has a method to compare with objects of its own class, without any loopholes such the one above?

I obviously could't post the above as a comment hence I created a new post.

jahroy

You are correct: this idiom does not prevent classes from being compared to different classes. All it does is ensure that the compared object also implements the same interface. If there is a requirement to only compare the same types, that can be enforced by the implementing class.

What you call a "loophole" is what I would call "intentionally doing something you don't want to do".

Foo objects can be compared to A objects IF such behavior is desired.

This is a feature, not a loophole.

If you want Foo to be comparable to other Foos, you should define Foo to implement IComparable<Foo>.

If you don't want Foo to be comparable to A, then you shouldn't define Foo to implement IComaparable<A>. Why would anybody do that unless they were trying to write broken code on purpose?

The actual answer to your question has already been provided by @caskey:

"No, you can't do what you want using interfaces in Java. [You have to do it with classes]."

There is one thing that you missed:

Therefore (unless I've misunderstood something) the original idiom doesn't really buy anything more compared to the far less dramatic:

public interface IComparable<T>

The original idiom does buy you something. It enforces that the compared object must implement IComparable. The less dramatic example would allow you to compare implementing classes to any object without restriction. So... The compiler would allow you to specify Long, or InputStream, or LinkedHashSet<Byte[]>, or anything at all as a type parameter.

When you look at it that way, it's easy to see why this idiom is so common.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Arguments in a method request a constant from an Enum - How can these methods only accept specific constants?

How can I make an interface instance method accept arguments of the same class only?

How can instance of interface access method of Object class?

How can I make a textbox only accept a valid email?

How can I make the interface to my enumerator accept feed?

In python, how can I inherit and override a method on a class instance, assigning this new version to the same name as the old one?

How can I make a Class with reference data member constructible with no arguments?

How do I do create an instance of the same class that the method belongs to?

How can I make only the parent method run in a class inheritance on PHP?

How can I make a JavaScript class instance be instance of another class?

How can I make a class instance from a list?

How can I make tuples an instance of this class in Haskell?

How can I create a interface that receives a class, and not a instance of the class in typescript

How can I limit a method to only accept parameters with certain restrictions?

When using the 'Class' datatype, how can I specify the type so I only accept subclass of a specific class?

How to make an accept condition for droppable td to accept only the class within the same row?

How can I call an instance on a former instance from the same class?

How can I make input field accept INTEGERS only with AngularJS

How to make a class-instance accept parameters?

How can I use a static method in an interface to return a generic instance returned by an instance method?

How can make an instance class require a method call?

How can I make a method accept a class type variable in Java?

How to define one method as both instance method and class method (sharing the same name), each with different arguments?

How can I make a list defined in one method available in another method when both methods are in the same class?

Created a Task interface with an execute method. How can make it accept paramenters dynamically - JAVA

How can I make instance of form1 in a class and make instance of the class in form1?

How can i make a method available only from within the class

How can I make 'this' equal to another instance of the same class in Dart?

How can I make my class instance serializable for multiprocessing in Python?