Java uses .class
file format. Compiler gathers information about class and puts it into .class
file. In a result linking can be done without involving source files. Note that in Java you have one public class per file requirement, so that for each source .java
file one .class
is generated.
In C++ code is usually compiled to object
files, which have no idea about high level concepts such as classes or structures. Therefore information has to be provided in header files, so that compiler can perform checks (or even to know how to generate some code), before going to link phase.
I was looking for rationale behind forward declarations, but ANSI C by K&R (it's probably a "feature" inherited from C), just states that every variable must be declared before use and that ANSI C made forward declarations of functions consistent with definitions (they were not required to be compatible earlier).