The java code I\'m working on at the moment has often a structure like
file Controller.java:
interface Controller {...}
file Controller
In Java, an interface defines a contract, while a class provides an implementation of a contract.
Most contracts have only one meaningful or relevant implementation; some even assume a specific implementation and do not allow any other. Those contracts and their implementations are defined together in classes, without any interfaces. Example: java.lang.String
.
On the other hand, some contracts do not make any assumption on the possible implementations. Those are defined in interfaces. Partial implementations can be defined in abstract classes, and typical complete implementations can be defined in classes, but the fact that the contract is defined in an interface allows you to write your own implementation of that contract and use it wherever an instance of the contract is expected.
At the file level, both an interface and a class are compilation units and deserve their own file.
As you now hopefully understand, the distinction between header files and implementation files in C++ is very different.
No, programming against interfaces has nothing to do with whether or not you use header files.
Defining an interface explicitly will allow evolving the program on both sides independently. The 'interface' of a code body denotes the contract to which it obeys.
Programming languages never fully support interface definitions, they merely have some syntactic sugar to handle the type-correctness aspect. Performance aspects, for one, are most of the time unspecificable, just like complexity rules etc... (C++ standard defines some complexity requirements for the containers and algorithms).
This limited support has left Java with the keyword interface
: since object orientation is about grouping functionality into classes, it made sense to couple the concept of an 'interface' to something that groups member function definitions. Several classes can implement a given interface, and a class can implement many interfaces as well.
In C++, they didn't even bother to explicitly add 'interface' to the language. The thing that comes closest to the 'class interface' of Java is a pure abtract class: a class with only pure virtual member functions. Since C++ supports multiple inheritance, classes can implement multiple of these 'interfaces'.
When it comes to the separation of your code into headers and source files, that's totally irrelevant to the interface concept. But indeed: in C++, the 'calling contract' is mostly specified in a header file. And indeed, that makes for shorter compilation times (less IO). But that's a compiler-technical aspect.
Header files in C/C++ has nothing to do with classes or interefaces at all.
A header file is more like the reference you add to your poject or the using statement.
Its an instruction to the compiler that the objects and function declarations in the header file exists so that the compiler can compile the file without having to look through the whole project.
A header file in C++ can Contain classes or function definitions or macros or enums or many other things but is conceptually very different from classes or interfaces.
You can use interface in C++ to How do you declare an interface in C++? and those definitions are placed in header files, but the header file in it self is something else.
The reason to use interface instead of inheritance is that many classes can implement an interface or many interfaces but you can only inherit from one class.
So if you have many objects that is to be interchangeable you would end up with a very complex class hierarchy while with interfaces the classes can be built completely separate with only the interface linking them .
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 are more like abstract base classes in c++.
the reason they are often used in java is that multiple inheritance does not exist in java (both were design decisions). c++ supports multiple inheritance, so... it can accomplish the problem that way (and via a few others).
once a class implements an interface in java, then it can be passed as the type. the same principle in c++, although the written implementation is slightly different.
so, the interface simplifies the programming process, since arbitrary objects may be passed, as long as they implement the interface (or in c++ are derived from a specific class). with an interface, you do not need to derive from a common base class -- it's a very simple, and usable design. multiple inheritance in c++ is a pitfall for many developers.
The question is good, there is a connection between header files and classes/interfaces/OO programming, beyond the raw syntax of the languages.
Proper C++ program design:
Proper Java program design:
Proper C design:
If you don't use the above intimate connection between classes and h-files, there is a fine chance that your program is an unreadable mess, no matter how elegant your OO design is.