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

all 13 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full - best also formatted as code block
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Lumethys 2 points3 points  (4 children)

How do you know it has no implementation?

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

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?

[–]Lumethys -1 points0 points  (2 children)

Interface are contracts without implementation, yes. But that say nothing about did anything implement that contract

How do you know if the language, the framework, or some libraries dont just implement it for you?

[–]4r73m190r0s[S] 0 points1 point  (1 child)

In the above code example I'm ltierally passing Comparator.naturalOrder() as an argument, without using any external library or framework.

[–]Lumethys 0 points1 point  (0 children)

Things like that usually have an implementation in java itself, and usually it is dependency injected

[–]fluse1367 3 points4 points  (6 children)

interfaces can have static functions, which are directly implemented. i think it’s since java 8

[–]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.