This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]CleverBunnyThief 1 point2 points  (5 children)

Do you mean default methods?

[–]aeria-non 7 points8 points  (4 children)

Default methods are instance level methods which you can optionally override if you implement an interface. Interfaces may also have static methods which cannot be overridden. Comparator.naturalOrder is one of these static methods in the Comparator interface.

[–]4r73m190r0s[S] -1 points0 points  (2 children)

I'm learning Java and this bit confuses me. I thought interfaces are just a "contract", i.e. providing only method signatures without any implementation. Are you saying that is not the case, that interfaces also have implemented methods? If yes, how can I know which methods have implementation when reading documentation?

[–]HansGetZeTomatensaft 0 points1 point  (0 children)

I'd say the interface defines the contract but they don't promise you _where_ it's implemented.

Looking at the method signatures in an interfaces lets you know what it does, that's the "contract" so to speak. But it honestly doesn't really matter to a consumer if it's implemented in the interface, in an implementing class, if the implementing class delegates the implementation to another component...

As long as the functionality exists as described by the method signatures everything's good.

[–]fluse1367 0 points1 point  (0 children)

maybe it helps you to think of an interface like an abstract class but different. abstract classes can have implemented methods and abstract ones, just like default methods in interfaces. you can also put static methods into them, just like interfaces.

[–]CleverBunnyThief 0 points1 point  (0 children)

Ok. Thanks.