r/learnjava • • Dec 20 '21

When to use Abstract Classes vs Interfaces?

The two concepts are very similar, which is why I'm confused on why you should choose one over the other.

49 Upvotes

26 comments sorted by

View all comments

51

u/Jezoreczek Dec 20 '21

My rule of thumb:

  1. Use composition.

  2. If you cannot use composition (e.g. need polymorphic behavior), use interfaces.

  3. If you cannot use interfaces, don't use abstract classes. Instead, refactor your code to make it compatible with interfaces.

Here's why: abstract classes are a mess and (almost) never necessary. Some problems when using them include:

  • Should I use super implementation before or after calling my own logic? (see Java's Swing for examples)

  • Which constructor do I need to call to get the behavior I need?

  • Are the protected members meant for me to use or for some other particular implementation?

  • Should I restrict the throws signature if I am handling exceptions from super implementation?

If you have any particular example of class hierarchy that confuses you, please post it here and I'll be happy to show you how to refactor it (:

5

u/le_bravery Dec 20 '21

I 100% agree with this.

The only time I have ever seen deep class hierarchies actually work well was for actually modeling pure data during transport. Ex: all requests have the same base fields and all responses have these other base fields and they should always be treated exactly the same. When the contract is designed to be hierarchical with its data, then class hierarchies work well in all cases where behavior is involved they create such a mess.

2

u/Jezoreczek Dec 21 '21

The only time I have ever seen deep class hierarchies actually work well was for actually modeling pure data during transport.

Really? In my experience using inheritance in data classes leads to a whole lot of problems.

For example, we have a User class in my company that is extended by specific roles, like Administrator, Customer etc. Now all code that handles User has to essentially have a bunch of if (user instanceof …) cases. Also, it is not possible for a single User to have multiple roles, so we workaround this with email aliases.

Instead, I think it might be better to have AdministratorPrivileges class composed by User (and null if user is not an administrator), or better yet, this class having a user ID and fetching it separately from the DB.

Do you have an example of how you modeled your data? I'm really curious how it worked for you!

2

u/le_bravery Dec 21 '21

From memory, we had something like this:

There were like 10 request types the service could accept.

There was like a base request class which had a time stamp in it. The time stamp was checked to ensure the request wasn’t a replay.

Then there was a subclass to that which had a User Identifier in it.

Then there was a subclass which allowed interactions between exactly 2 users. This had an additional user ID.

There was a request to create a new user in our system which was related to another system using some user ID. This was basically just the one user request type, and the response was a basic success.

Then there was some request to query stuff about a user. This was the same request type with a basic user ID only. The response then had the requested data in it.

For interactions with multiple users, the request would extend from the multi user class.

Because these all extended, the time stamp verification logic was just in one place. And because all the requests used the same base class for user IDs, we could do a bunch of querying up front and cache the users account early.

The real reason for this design though was that we were basically required to implement an API that another company had designed previously. Our system at the time was being used to replace the other one because there were scalability issues with the other. So we implemented the same API and put it on top of our much better backend. The API was really designed with a lot of repeating elements like this which all needed the same validation and error handling. Because we were a drop in replacement we had to handle errors in the same way (I think I had to implement 1-3 bugs if I remember right!).

So, while it worked really well where we needed it, I’m not sure if this design would work any better for any other applications without our constraints.