I have been a developer for past 3 years and I have been seeing interface
in most of the places as a contract for the developers to write their own implementation or a marker (eg. Serializable
). But I quite dont understand how this concept is named as INTERFACE in literal meaning. Am I missing something pretty basic?
-
What is the literal meaning of the word interface? For most of us the word fits the concept very well.– BentCommented Jan 9, 2017 at 14:51
-
But the question is, I dont see interfaces in Java being used for interfacing... Then why?– Raja AnbazhaganCommented Jan 9, 2017 at 15:09
-
If you have an object that implements an interface, then you can call the methods on that object that is defined in the interface and implemented in the object's class. When you call those methods, is that not interfacing with the object?– BentCommented Jan 9, 2017 at 15:29
-
2@RajaAnbazhagan: what is your personal definition of "Interfacing" then?– Michael BorgwardtCommented Jan 9, 2017 at 15:38
-
2The point you seem to be missing is that you do not have to know what class an object is, you can use the methods as long you know that the object implements an interface. You can have a collection of objects that all implements the same interface but are from classes that do not have anything else in common than the interface. Then you can iterate over the collection and call the methods from the interface. I think you could benefit from reading up on programming to an interface not to an implementation to learn the strengths of interfaces.– BentCommented Jan 9, 2017 at 19:24
2 Answers
Interfaces are the points in our world that describe how things interact with each other. User interfaces let users interact with applications, machines, and so on.
In programming interfaces are to be understood as the pre-defined interaction surface between two pieces of a program, or even multiple programs (hence API).
Interfaces are, of course, different in all programming languages but usually they operate on the same principle.
OK, based on this comment you made:
aren't classes and methods are also interfacing with Other objects...?
I think the point where you're getting confused is thinking that because it's called "interface" it does something that regular classes don't. But of course, any class's public members are effectively an interface in that sense.
The reason why Java interfaces are called so is that they are only an interface.
And they are most definitely used for interfacing in the sense of "a point where two systems, subjects, organizations, etc. meet and interact". For example, java.sql.Driver
is an interface used to talk to an SQL database via JDBC.
But the concept is also useful within a system for more low-level interactions and since there are more of those, it gets used more often for that.