The java code I'm working on at the moment has often a structure like
file Controller.java:
interface Controller {...}
file ControllerImpl.java:
class ControllerImpl implements Controller {...}
But for every interface there is only one implementation. Isn't that the same as using header files in C/C++ where I have the code split into files like
Controller.hpp
Controller.cpp
From what I know, header files in C/C++ have been introduced to help the compiler, which is not necessary anymore in Java. Also header files should help with the readability of the code, but having a modern IDE with folding and outline view this is also not a necessity anymore.
So why do people again introduce header files in Java through the back door by programming against interfaces?
No. In C++, files (headers) are not the same as classes.
Programming against interfaces as in Java can be done in C++, too, by programming against abstract base classes.
However, the Java term of "interface" is quite restricted. Basically, any function declaration is an interface:
void call_me(int times);
As are, of course, classes and other types.
In C++, you group such things in headers, so an interface can consist of one header. However, it might just as well consist of multiple headers.
Interfaces doesn't come from a desire to keep header files.
Interfaces the closest Java comes to supporting http://en.wikipedia.org/wiki/Design_by_contract
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With