r/javahelp • • Jun 02 '21

When would you have a Class that extends an Abstract class that implements an Interface?

Interface -> AbstractClass -> Class

21 Upvotes

11 comments sorted by

•

u/AutoModerator Jun 02 '21

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://imgur.com/a/fgoFFis) 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.

19

u/[deleted] Jun 02 '21

Example:

Clickable -> Abstract Button -> Check Button

Ie top level is something that clicks, we define a class of buttons that click, and an example which is an button that has a check mark.

This is a contrived example. But interfaces help hide the implementation details, an abstract class doesn’t really since you are constrained to a specific hierarchy.

2

u/umlcat Jun 02 '21

Very good and simple practical example.

Like to complement that both the Interface and the Abstract Class may have several implementations like:

Clickable -> Window -> Dialog

Clickable -> Button -> RadioButton

5

u/[deleted] Jun 02 '21

This was a common practice before Java 8 introduced, default methods, so you could make the interface more convenient to use by having an implementation that partially (or totally) implemented the interface with default (or do nothing) implementations.

These classes are often called "Adapters".

However, a default implementation could still be useful if you want to provide state or private methods, which are not possible with default methods in interfaces (which can only refer to other interface methods).

5

u/snoob2015 Jun 02 '21 edited Jun 02 '21

It is a very common pattern. Interface is for other to use it, Abstract class is for you to extends it. For example:

Client ----> List (Interface) -> AbstractList (Abstract Class) -> List Implementations.

So what is the difference between Interface and Abstract Class conceptually:

- Interface is what other expect you to do ( A list is something other people use to store elements). If your class implement List, that means your class can store elements.

- Abstract Class is what you use to reuse code.

4

u/lengors Jun 02 '21

Personally, I have those scenarios when I want a set of Classes to share either a partial or full implementation of the Interface and that implmentation is not possible through the usage of default methods in the Interface.

3

u/[deleted] Jun 02 '21

The general rule of thumb that I use is to use abstract classes for state and interfaces for behaviour (never mind default methods et al). If you follow this rule of thumb, then the question becomes moot.

If you wish to have a hierarchy of related objects that must all implement some behaviours, then you have an abstract class from which you inherit your types, and then that abstract class implements one or more interfaces, which the child types now have to conform to.

1

u/wsppan Jun 02 '21

Interface is used when you want to define a contract and you don't know anything about implementation. (here it is total abstraction as you don't know anything.) Abstract class: Abstract class is used when you know something and rely on others for what you don't know. If you use a recent version of Java you can skip the abstract case and use default methods.

1

u/DubbaRubba Jun 03 '21

I do this often. In many cases is use Interfaces that describe a behavior or "function" then abstract classes as composites of those interfaces. Finally concrete classes extend those composites. Unfortunately, Java does not support multiple inheritance, therefore interfaces and through utilizing default methods can help in many ways.

0

u/bny-mobile Jun 02 '21

If you want all or most subclasses to share the implementation of the interface in question.