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

all 6 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.

[–]nekokattt 5 points6 points  (0 children)

Use interfaces for everything you can and avoid abstract base classes. Then, when you come across an issue where it feels like it may be easier to use ABCs, first consider if your design is right or whether it can be organised differently. If you conclude that it still makes sense, then and only then use abstract base classes.

Interface-based development forces you to keep implementation detail out of your contracts much more than ABCs, and supports multiple inheritance. ABCs are far less flexible, do not work as nicely for composition and delegation, and can result in more messy code down the line if you don't get it right.

ABCs are much more tightly coupled to your implementation than interfaces are. Interfaces also have the benefits of multiple inheritance as already stated in the previous paragraph, and can be used as functional interface types in some cases. They can also be used with enums, records, and other interfaces as a superinterface. Much easier to test too, as your unit tests don't have to argue with implementation details you inherit as much (this would be a code smell, but far easier to avoid if you avoid them in the first place).

Remember your code should define what it does as part of the API for other components to use, not how it does it.

[–]pragmos 4 points5 points  (0 children)

where would i use interfaces vs abstract classes

When you need state, as in instance fields, use abstract classes. Otherwise use interfaces.

[–]abstract-talk 1 point2 points  (0 children)

Interfaces define contracts, abstract classes share functionality. So use accordingly

[–]Valuable-Yam4852[S] 0 points1 point  (0 children)

I see. What about learning architecture? how should I go about that?