I am still not so familiar with interfaces in Java. I know that interface tells which methods should have classes that implements that interface. But what is sense? Why to use it? Is not easier to create instance of class that implements interface instead of instance of interface? Or do I something important miss?
Share
Many people use interfaces to define abstract classes – all the members of a class, without the explicit implementation. I frankly don’t see a lot of value in that.
I prefer to use interfaces to do one of two things:
The second one is, I think, the more interesting, and probably deserves more explanation. You might write a class that requires teh ability to load or save some data. Rather than directly talking to whatever data store you’ll use, you can declare an interface that has the methods you want to be able to call.
Then, implement a class that has those methods (implements that interface), and pass it to the class that wants it.
This gives you good abstraction and separation of responsibilities.