r/learnjava • u/MSUC123 • 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.
9
4
u/sweetno Dec 20 '21
You'll have to use abstract classes if there are instance variables.
Multiple inheritance is only possible via interfaces.
1
Dec 20 '21 edited Dec 20 '21
Interfaces can be used like instance variables, it just implies an anonymous implementation of the interface (just as instantiating an abstract class instantiates a concrete anonymous subtype).
Example:
interface Animal { void talk(); } public static void main(String[] args) { HashMap<String, Animal> animals = new HashMap<>(); animals.put("Duck", new Animal() { @Override public void talk() { System.out.println("Quack"); } }); animals.get("Duck").talk(); }This creates an anonymous (i.e. unnamed) concrete class which implements Animal, like you would do by saying
class Duck implements Animal. Since it's an interface with a single method (which is implicitly abstract), you can also simplify with a lambda, likeanimals.put("Duck", () -> System.out.println("Quack")); animals.get("Duck").talk(); // Quackedit: Not sure why it's downvoted, maybe I should put 'instantiated' in quotes or something, but I thought it was clear that what actually gets instantiated is an anonymous concrete class which implements the interface.
1
u/Jezoreczek Dec 21 '21
I believe they meant the fields of the abstract class when they said "instance variables" (;
1
1
u/dhawal0008 Dec 20 '21
Suppose you are writing a code for RBI and RBI share this code with other banks. Now the generic methods in this class would be - Add User, Deposit, Withdraw, Inquiry. All these will be abstract methods.
Now suppose SBI also use this code and they have come up with a wallet scheme. Now since this wallet scheme is exclusive to SBI, they will write a interface which will be used by them only.
Similarly other banks, with their own added features can create interfaces.
Common functionalities among various classes - Abstract class/methods Feature exclusive to certain classes - Interface
1
Dec 20 '21
One use case for an abstract class rather than interface would be if you didn't want it to have only abstract methods. An abstract class can have abstract or concrete methods, whereas interface methods are all implicitly abstract.
1
u/Jezoreczek Dec 21 '21
interface methods are all implicitly abstract
What about
defaultmethods?1
Dec 21 '21 edited Dec 21 '21
Good point. Default methods don't work the same way as regular 'concrete' methods though, because you wouldn't define those in the implementation class, they would already be defined with an implementation in the interface. [Though I guess the abstract class could have non-abstract method which would have an implementation in the class. Still, there are differences, like that the interface could be a functional interface, so maybe there should be different use cases for one or the other along those lines.]
51
u/Jezoreczek Dec 20 '21
My rule of thumb:
Use composition.
If you cannot use composition (e.g. need polymorphic behavior), use interfaces.
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
superimplementation 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
protectedmembers meant for me to use or for some other particular implementation?Should I restrict the
throwssignature if I am handling exceptions fromsuperimplementation?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 (: