Does it make sense to override a method in an interface

stonar96 :

I have for example an Interface A and B. A has an (abstract) method called foo. B extends A.

It is possible to override foo in the interface B even with @Override, but is there any situation where that makes sense? There is nothing to override, because both methods have to be abstract and have no body. So I guess there is no situation where this makes sense, right?

So why is it possible to override in an interface?

M Anouti :

One situation is when you want to update the Javadoc documentation to reflect a more specific contract in the method in the subinterface, as is the case with Collection#addAll(Collection) and List#addAll(Collection):

  • Collection#addAll(Collection):

    Adds all of the elements in the specified collection to this collection (optional operation)...

  • List#addAll(Collection:

    Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation)...

A subinterface can also add a default implementation starting Java 8.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

interface method that does not make sense for all implementing classes

Does pointer to interface make sense?

Does this generic method signature make sense?

Does this method from the JDK make sense to anyone?

Why does the empty method appendTo make sense?

Method is not abstract and does not override method in Interface error

If a method only has assignments, does it make sense to await that method?

Does it make sense to create a `Copyable` type interface instead of using `Cloneable`?

Does it make sense to use interface when there are different generic types

Does abstract class hide or override the method of the interface?

Does using ReadWriteLock inside a synchronized method make sense?

Does a singleton with only one public method make sense?

Does this async method using TaskCompletionSource make any sense?

Does it make sense to pass in all the properties of a class as arguments in a constructor method?

Method does not override method from its superclass (Interface) java

JAVA: override interface method

Does it make sense to mix regularizers?

Output of program does not make sense

DACPAC file does not make sense

Validation accuracy does not make sense

inline this function, does make sense?

Does -o and -i make sense when the target host is already connected to the interface specified?

Does it make sense to have a non static method which does not use an instance variable?

Mocking interface doesn't make sense?

Does it make any sense to make struct immutable?

Does it make sense to make this aggregate roots design?

Does it EVER make sense to make an array property?

Does it make sense to use Spring's @Transactional annotation in a method which doesn't talk to database?

Does it make sense to write addition test for a method if it is already covered by another test?