If you leave the method overriding the image, you might think of Java classes and methods as C-style struct pairs and a set of functions that work with these struct s. For example, if you have a class like this:
public class MyJavaClass { private int x; public int getX() { return x; } public int setX(int value) { x = value; } }
It will be like writing C code in this regard:
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 basic idea is that the method is similar to a function that takes a receiver object as an implicit parameter of "this". In C, you must explicitly pass the receiver as a parameter to the function, while in Java this is done implicitly through the syntax object.method() .
If you start introducing a method override, it becomes a little more complicated because the method you call on the object depends on the dynamic type of the object, and not on the static type. One way to simulate this is to use something called the vtable virtual table table or the so-called because of the C ++ virtual . The idea is that each object stores a pointer to a table of function pointers, one for each function that can be overridden, and when a method is called on an object, the corresponding function pointer is selected from the table and called. So, more correctly, the above Java object might look something like 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; } struct MyJavaClass_Vtable MyJavaClassVtableInstance = { &MyJavaClass_getX, &MyJavaClass_setX };
Whenever you create an instance of MyJavaClass , you must configure it vtable as follows:
struct MyJavaClass* mjc = malloc(sizeof *mjc); mjc->vtable = &MyJavaClassVtableInstance;
Then, when calling such a function (in Java):
myJavaClass.getX();
In C, it will look like
myJavaClass->vtable->getX(myJavaClass);
So, in a way, the Java class is just a structure with some extra meta information. Of course, for a programmer, it looks completely different: encapsulation, polymorphism, a more stringent type system, etc., But at the native code level, the regular C structure and Java class probably look very similar.
Hope this helps!