Did Java invent interfaces?
I know about C++ pure virtual classes, but Java went one step further and created a first-class (no pun intended) concept for multiple-interface (not implementation) inheritance, 开发者_StackOverflowthe interface
. It's now a staple of major statically-typed languages. Did Java invent the interface concept? Or did it appear in older languages also as a first-class concept?
Here is abstract from the article The Java Language Environment.
An interface in the Java language is simply a specification of methods that an object declares it implements. An interface does not include instance variables or implementation code--only declarations of constants and methods. The concept of an interface in the Java language was borrowed from the Objective-C concept of a protocol.
http://java.sun.com/docs/white/langenv/Object.doc2.html#6185
As far as I know Objective-C was the first language to feature interfaces. At least it had them before Java was invented.
I'm pretty sure IUnknown and IDispatch existed before Java... But that's COM, which isn't really a language.
pure virtual classes without fields were called in C++ interfaces even before Java. Idea behind Java's interfaces was to remove nasty multiple inheritance as it was in C++, but leave similar capabilities.
I'm quite sure that Java developers didn't invent the concept of interfaces but they popularize them.
IDL, used by CORBA, I believe goes back to the early 80's.
I love interfaces but I am just at the beginning of learning how to use them. The most flexible and powerful interface system that exists in a programming language is in Go, but as people pointed out, Java popularised them but they existed 20 years ago, I wish I could remember the language I found the other day, starts with 'g' I think, maybe something about a mineral or flower in the name.
The central principle of interfaces is about creating descriptions of things around how they pass values between. They create a way to pass a chunk of data with some set of uses to another form with a very close set of uses. Interfaces usually use a naming scheme to indicate that the interface definition pivots around the abstract commonality between several things. In programming, everything boils down to bytes, so you can build interfaces around sharing bytes, there is always some common feature.
In a practical sense, an interface is a set of methods surrounding a data type that you haven't specified, except vaguely. Go will teach you good about interfaces. Interfaces appear most prominently in the most object oriented language, as the OP, java, but as far as I know, Go has the most powerful interface system. Go is statically typed, but interface types make no constraints on data, in Go. Only the limitations you define by the types the interface can pass.
I am just starting to learn how to program in Go with interfaces, but a lot of the wisdom gained by the Go programmers' experience is helpful with anyone interested in interfaces. Interfaces need to be compact and functional. Very often, you don't need to define an interface because the data is just too different. Maybe for storage and transmission, all data === all data. This is what interfaces are for. Two different systems, what data structures do they share. This is exactly what an interface is.
精彩评论