Possible Duplicate:
The purpose of interfaces continued
I just started learning Java not so long ago.
I’ve come across Interfaces which I know how to use but still can’t quite grasp the idea of it.
As I understand it, interfaces are usually implemented by classes, which then have to implement the methods declared in the interface.
The problem is – what exactly is the point? Wouldn’t it be easier to just implement the methods from the interface as normal class methods? What exactly are the advantages of using interfaces?
I’ve tried looking for an answer on google. There were many but I still couldn’t understand the point of it. I also read this question and its answers, but the whole contract thing just makes it more complicated…
Hopefully someone could simplify it good enough! 🙂
Thanks in advance!
An interface allows you to provide a different implementation at runtime, inject dependencies, separate concerns, use different implementations for testing.
Just think of an interface as a contract that a class guarantees to implement. The concrete class that implements the interface is irrelevant. Don’t know if this helps.
Figured some code might help. This doesn’t explain everything there is more to interfaces, keep reading but I hope this gets you started. Point here is that you can vary the implementation…