[ACCEPTED]-What is the difference between C structures and Java classes?-c
If you leave method overriding out of the 36 picture, then you can think of Java classes 35 and methods as a pair of a C-style struct
and 34 a set of functions that operate on those 33 struct
s. For example, if you have a class like 32 this:
public class MyJavaClass {
private int x;
public int getX() {
return x;
}
public int setX(int value) {
x = value;
}
}
This would be similar to writing C 31 code to this effect:
struct MyJavaClass {
int x;
};
int MyJavaClass_getX(struct MyJavaClass* this) {
return this->x;
}
void MyJavaClass_setX(struct MyJavaClass* this, int value) {
this->x = value;
}
The main idea is that 30 a method is similar to a function that takes 29 the receiver object as an implicit "this" parameter. In 28 C, you have to explicitly pass the receiver 27 as a parameter to the function, while in 26 Java this is done implicitly through the 25 object.method()
syntax.
If you start introducing method 24 overriding, this becomes a bit more complicated 23 because the method that you invoke on an 22 object depends on the dynamic type of the object, not 21 the static type. One way of simulating this is 20 using something called a vtable or virtual function table, so named because 19 of C++'s virtual
keyword. The idea is that each 18 object stores a pointer to a table of function 17 pointers, one per function that can be overridden, and 16 when a method is called on the object the 15 appropriate function pointer is selected 14 out of the table and called. So, more properly, the 13 above Java object might look something like 12 this:
struct MyJavaClass_Vtable {
void (*getX)(struct MyJavaClass* this);
void (*setX)(struct MyJavaClass* this, int value);
};
struct MyJavaClass {
struct MyJavaClass_Vtable* vtable;
int x;
};
int MyJavaClass_getX(struct MyJavaClass* this) {
return this->x;
}
void MyJavaClass_setX(struct MyJavaClass* this, int value) {
this->x = value;
}
/* A global instance of the vtable for MyJavaClass */
struct MyJavaClass_Vtable MyJavaClassVtableInstance = {
&MyJavaClass_getX,
&MyJavaClass_setX
};
Whenever you created an instance of 11 MyJavaClass
, you'd set up its vtable doing something 10 like this:
struct MyJavaClass* mjc = malloc(sizeof *mjc);
mjc->vtable = &MyJavaClassVtableInstance;
Then, when invoking a function 9 like this (in Java):
myJavaClass.getX();
In C it would look like
myJavaClass->vtable->getX(myJavaClass);
So 8 in a sense a Java class is just a struct 7 with some extra metainformation. Of course, to 6 the programmer it looks totally different 5 - there's encapsulation, polymorphism, a 4 stricter type system, etc. - but at the 3 level of native code a regular C struct 2 and a Java class probably look very similar.
Hope 1 this helps!
C
struct cannot have methods/functions in 3 it. It is only a collection of different 2 data types. class
can have both variables and 1 methods declared.
A Java class lets you define fields, like 14 a C struct, but has the following additional 13 features:
- As you state in your question, you can add methods to a class which can operate on the data.
- You can declare both fields and methods
private
(and a few other access settings), which means only other methods of the class can access them, not code from outside. (It wouldn't make sense to have this on structs, since there would be no way to access private fields -- this only makes sense when you allow methods). - Classes can inherit from other classes. Among other things, this lets you pass an object of type B where a type A was expected, as long as B inherits from A.
There is another subtle difference. In 12 C, structs are value types but in Java, classes are 11 reference types. That means when you copy a struct in C 10 (such as assigning to a variable or passing 9 as an argument), it gets all of its fields 8 copied, whereas in Java, when you copy a 7 class object, it just copies the reference to the 6 object (so if you modify its fields, you 5 modify the original object as well, not 4 just the copy).
In C, you often use pointers to structs 3 to emulate the reference behaviour of Java. In 2 Java, you don't use pointers because class 1 objects are already references.
The key differences, IMHO are that classes 2 allow
These are two very important features 1 of OOP.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.