In interview I have been asked following question. I tried to answer the question but I want exact answer of the question.
If I can simulate Abstract class as Interface, why java provided Interface?
This mean if in Abstract class I can mark all methods as abstract and then abstract class will work as interface, so why I need interface.
Can anyone explain me in brief.
That’s a very standard interview question. The answer is: because you can implement multiple interfaces, but can’t extend multiple abstract classes.
Example from the JRE:
LinkedListis both aListand aDeque. These interfaces define the behaviour of the class. They do not provide any implementation details. While abstract classes could provide some.Related questions: this and this. The latter is not directly related, but it shows why are interfaces needed, even in cases when an abstract class would suffice.