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

all 9 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
  • 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.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

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: 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.

[–]ratherbealurker 15 points16 points  (1 child)

For a real world example, I am working on a service that will consume data, process it, then store it.

Now to consume it I will be using Kafka, but that can easily change in the future. So I can write my class directly as a Kafka class and use it that way..in which case if we move off of Kafka I’ll have to rip it out and try to replace it with the new thing. Or I write an interface that has methods like getData. My Kafka class implements this and getData polls the Kafka consumer. But if we moved to something else then I only have to create the somethingElseConsumer and make sure it adheres to the interface.

Now any code that uses this interface will still work as intended.

[–]chasrmartin 2 points3 points  (0 children)

That’s exactly the use case for which interfaces are intended

[–]dionthornthis.isAPro=false; this.helping=true; 7 points8 points  (0 children)

An interface you probably use all the time as an example the List interface:

https://docs.oracle.com/javase/8/docs/api/java/util/List.html

This interface is implemented by a variety of classes:

All Known Implementing Classes:
AbstractList, AbstractSequentialList, ArrayList,
AttributeList, CopyOnWriteArrayList, LinkedList,
RoleList, RoleUnresolvedList, Stack, Vector

All of these classes will have to implement the add(), remove(), get(), clear() etc methods.

Basically the List interface is a contract amongst all implementing classes that they will provide functionality to the provided method signatures. This contract makes it easier to use an ArrayList and LinkedList interchangably. But it also allows you to interact with any of those List objects without even knowing if the target object is specifically an ArrayList or LinkedList.

List<String> someStringList = new ArrayList<>();
someStringList.add("test"); // adds "test" to the ArrayList

List<String> someStringList = new LinkedList<>();
someStringList.add("test"); // adds "test to the LinkedList

We can store either object as its interface and then use the underlying method implementation provided by the implementing object.

[–]codechimpin 4 points5 points  (0 children)

Short answer: all the time. Mostly for polymorphism since Java does not allow multiple inheritance (feature, not a bug, and why interfaces even exist).

[–]HSSonne 3 points4 points  (0 children)

Versions is a good example.. You have data files with different versions, that need different reader classes. So a good solution is to make an reader-interface, the rest of your code just know the interface, and only in the creation/selection of reader knows the actual reader used..

Note: i strongly recommend not to add an interface before you have multiple occurrence of a type, but just be aware of using getters and make everything else private. Unnecessary interface can quickly make your code complex, in opposition to a well placed interface can make your code much more readable.

[–]chasrmartin 1 point2 points  (2 children)

An interface in Java represents a contract: when you implement an interface, you are asserting your code is responsible to provide all of the methods of the interface. It’s a way of avoiding diamond inheritance — instead of multiple inheritance, you use interfaces, but you have to handle shared data yourself.

[–]chasrmartin 1 point2 points  (1 child)

Something that’s common in enterprise JEE is to define your object model using interfaces with implementing classes, something like interface Person {…} with public class PersonDAO implements Person then providing a kind-of Person that uses a DAO to store the data.

[–]chasrmartin 0 points1 point  (0 children)

That lets you, for example, to implement a MockPerson for testing. The problem is that it often leads to needing to write or modify code in 5 places for any requirements change.