tags:

views:

291

answers:

8

I have a fair amount of practice with Java as a programming language, but I am completely new to C. I understand that a header file contains forward declarations for methods and variables. How is this different from an abstract class in Java?

+2  A: 

Here's the answers:

  1. Java has had a bad reputation among some hardcore C programmers mainly because they think:

    • it's "too easy" (no memory-management, segfaults)
    • "can't be used for serious work"
    • "just for the web" or,
    • "slow".

    Java is hardly the easiest language in the world these days, compared to some lanmguages like Python, etc.

    It is used in many desktop apps - applets aren't even used that often. Finally, Java will always be slower than C, because it is not compiled directly to machine code. Sometimes, though, extreme speed isn't needed. Anyway, the JVM isn't the slowest language VM ever.

  2. When you're working in C, there aren't abstract classes.

    All a header file does is contain code which is pasted into other files. The main reason you put it in a header file is so that it is at the top of the file - this way, you don't need to care where you put your functions in the actual implementation file.

    While you can kind-of use OO concepts in C, it doesn't have built-in support for classes and similar fundamentals of OO. It is nigh-impossible to implement inheritance in plain C, therefore there can never actually have OO, or abstract classes for that matter. I would suggest sticking to plain old structs.

    If it makes it easier for you to learn, by all means think of them as abstract classes (with the implementation file being the inheriting class) - but IMHO it is a difficult mindset to use when for working in a language without explicit support of said features.

    I'm not sure if Java has them, but I think a closer analogue could be partial classes in C#.

Lucas Jones
I don't know C, I don't know Java, but I know enough of them to know this is a crappy answer.....
Colin
I don't see anything shocking here.
Bastien Léonard
In what way is that a crappy answer? :) I'm perfectly ready to accept that it's crap and remove it from the interwebs forever, but I'd like to know what's wrong with it.
Lucas Jones
Java is definitively slow for GUI Desktop Applications. Maybe thats why most people coming from other languages say its slow, they're trying it for the wrong propose.
Havenard
A) java does not have a bad history B) just because you are using alanguage like C that does not natively support classes, there is no reason at all to forget the concept
anon
The line "basically forget all about classes" is what strikes me as wrong. C is OO isn't it? THe fact that there are no abstract classes doesn't mean there is NO OO does it? If I code a class names Car it can still have a collection of type Wheels can't it?
Colin
OK, I'll change that bit. Also, @Neil: it has bad history with relatively few C programmers. I'll edit that as well. Thanks for the feedback!
Lucas Jones
The answer not THAT crappy... +1
Subtwo
@Colin: C is not OO. C++ is but that an entirely different beast...
Subtwo
@Colin: no, C isn't OO. (http://userpage.fu-berlin.de/~ram/pub/pub_jf47ht81Ht/doc_kay_oop_en).
Bastien Léonard
Subjectively what I've experienced is that the tradition of hard-core old timers is that Java is surely a baby-talk language.
Subtwo
Even if C doesn't support OO programming directly, OO programming is more than just language support. OO programming is also about design (class hierarchies). Besides, many OO practices can be applied in procedural languages.
Inshallah
I hope the answer is a bit less, shall we say, offensive. Let's see if we can calm the ranting a bit now. :)
Lucas Jones
+2  A: 

If you forward declare something, you have to actually deliver and implement it, else the compiler will complain. The header allows you to display a "module"'s public API and make the declarations available (for type checking and so) to other parts of the program.

kaizer.se
Wrong: it is not the compiler that will complain. It is the LINKER that will complain.
Bruno Reis
Sure, to be exact it's the linker. I wanted to say "C will complain"; I mean it is not going to work.
kaizer.se
@kaizer.se: it might still link, if you provide the implementation elsewhere.
Bruno Reis
+4  A: 
  1. IMO it's mainly because many C programmers seem to think that Java programmers don't know how to program “for real”, e.g. handling pointers, memory and so on.

  2. I would rather compare headers to Java interfaces, in the sense that they generally define how the API must be used. Headers are basically just a way to avoid copy-pasting: the preprocessor simply includes the content of the header in the source file when encounters an #include directive. You put in a header every declaration that the user will commonly use.

Bastien Léonard
+1  A: 
  1. I think that there is much derision (mockery, laughter, contempt, ridicule) for Java simply because it's popular.

  2. Abstract classes and interfaces specify a contract or a set of functions that can be invoked on an object of a certain type. Function prototypes in C only really do compile time type checking of function arguments/return values.

Inshallah
+1  A: 

While your first question seems subjective to me, I will answer to the second one:

A header file contains the declarations which are then made available to other files via #inclusion by the preprocessor. For instance you will declare in a header a function, and you will implement in a .c file. Other files will be able to use the function so long they can see the declaration (by including the header file). At linking time the linker will look among the object files, or the various libraries linked, for some object which provides the code for the function.

A typical pattern is: you distribute the header files for your library, and a dll (for instance) which contains the object code. Then in your application you include the header, and the compiler will be able to compile because it will find the declaration in the header. No need to provide the actual implementation of the code, which will be available for the linker through the dll.

Francesco
+1  A: 
  1. C programs run directy, while Java programs run inside the JVM, so a common belief is that Java programs are slow. Also in Java you are hidden from some low level constructs (pointer, direct memory access), memory management, etc...

  2. In C the declaration and definition of a function is separated. Declaration "declares" that there exists a function that called by those arguments returns something. Definition "defines" what the function actually does. The former is done in header files, the latter in the actual code. When you are compiling your code, you must use the header files to tell your compiler that there is such a function, and link in a binary that contains the binary code for the function.

    In Java, the binary code itself also contains the declaration of the functions, so it is enough for the compiler to look at the class files to get both the definition and declaration of the available functions.

Zed
+2  A: 

Comprehensive reading: Learning C from Java. Recommended reading for developers who are coming from Java to C.

Harri Siirak
+4  A: 

The short answer:

Abstract classes are a concept of object oriented programming. Header files are a necessity due to the way that the C language is constructed. It cannot be compared in any way

The long answer

To understand the header file, and the need for header files, you must understand the concepts of "declaration" and "definition". In C and C++, a declaration means, that you declare that something exists somewhere, for example a function.

void Test(int i);

We have now declared, that somewhere in the program, there exists a function Test, that takes a single int parameter. When you have a definition, you define what it is:

void Test(int i)
{
    ...
}

Here we have defined what the function void Test(int) actually is.

Global variables are declared using the extern keyword

extern int i;

They are defined without the extern keyword

int i;

When you compile a C program, you compile each source file (.c file) into an .obj file. Definitions will be compiled into the .obj file as actual code. When all these have been compiled, they are linked to the final executable. Therefore, a function should only be defined on one .c file, otherwise, the same function will end up multiple times in the executable. This is not really critical if the function definitions are identical. It is more problematic if a global variable is linked into the same executable twice. That will leave half the code to use the one instance, and the other half of the code to use the other instance.

But functions defined in one .c file cannot see functions defined in another .c files. So if from file1.c file you need to access function Test(int) defined in file2.c, you need to have a declaration of Test(int) present when compiling file1.c. When file1.c is compiled into file1.obj, the resulting .obj file will contain information that it needs Test(int) to be defined somewhere. When the program is linked, the linker will identify that file2.obj contains the function that file1.obj depends on. If there is no .obj file containing the definition for this function, you will get a linker error, not a compiler error (linker errors are considerably more difficult to find and correct that compiler errors because you get no filename and line number for the resulting file)

So you use the header file to store declarations for the definitions stored in the corresponding source file.

Pete