Reputation: 135
I know that we can achieve 100% abstraction in Java with interfaces and partial abstraction with abstract classes.
In interview, the interviewer asked me to tell about any other way to achieve 100% abstraction except interfaces. Is there any other way?
Upvotes: 4
Views: 847
Reputation: 18803
One could use pure abstract classes with abstract methods only (no fields, no concrete methods).
Edit: Note that starting with the addition of default methods in Java 8, interfaces are no longer necessarily 100% abstract.
In the real world, abstract classes without fields (avoiding state distributed across the hierarchy) are probably more common than pure abstract classes.
Upvotes: 4
Reputation: 1
Use abstract classes that don't have implemented methods. These pure abstract classes like the interfaces has zero implementation.
If you want to learn about pure abstract classes and why they can be used instead of interfaces, you could read pure abstract class and interface.
Upvotes: 7
Reputation: 116
You have already got your answer i guess as mentioned by Stefan. However, I would like to add that the intention behind creation of abstract classes is to protect the developer to write the same methods for different classes and increase re-usability.
Upvotes: 1