[java] A couple general Java questions

Started by
4 comments, last by Diego 22 years, 7 months ago
I''m somewhat new to Java but I''m trying to put together a fairly basic game. First off, is there any equivalent to structures in Java? (or is that a complete no-no in OOP?) Also, what exactly is an interface? I tried to read up on it in the Java docs but the explanation gets a bit complicated. Thanks -Diego "What is wrong with the world today, the government, the media, or your family?"~Papa Roach
-Diego"What is wrong with the world today, the government, the media, or your family?"~Papa Roach
Advertisement
I don''t think java has anything like structures.
Use classes instead.

As for the interface:
interfaces are almost the same thing as abstract classes. You often use interfaces instead of abstract classes when the classes implementing the interface (deriving the abstract class) don''t have any implementation to inherit (variables etc).

Hope that clarifies.


/Mankind gave birth to God.
/Mankind gave birth to God.
Hi,

I try to answer to your questions somehow:

I would "translate" structures as classes. Structure contains usually just data - that you can do with a class. Class can offer you also means to manage the data (functions or methods).

Interface (abstract idea of it):
Imagine that you have lots of code (classes) implementing e.g. IP communication. To your actual application this would look like a mess (lots of classes, one handling the sockets, one handling socket listening, some class for threads, etc.). With an interface you can hide the mess from the other parts of your application.
In interface (class) you define ALL high-level methods + variables which are needed for the IP activity. Inside the interface class methods you call further the other classes (in the mess) as needed. This way you could modularize your code into modules with clear interfaces, thus allowing modules being developed separately (you can change the module contents/classes almost as much as you want as the interface hides them from the rest of the code).

Interface in java means that you declare certain variables and methods into a class. When you create a new class which "implements" the interface class, all definitions from the interface class are "added" into the new class. In addition you need to implement (in the new class) the declared methods found from the interface class...
This way you could also create an interface class containing all enums/definitions you need to "include" into all other classes in your application - similar to e.g. #include "constant.h" being added to all classes in C/C++.

Cheers,
Spec
There isn''t an exact equivalent of structures, as classes will have vtables.
Thanks guys, that cleared things up. I''m in the middle of taking a class here at school, and hopefully they will cover interfaces and abstract classes as that seems to be a real key piece of Java.

-Diego
"What is wrong with the world today, the government, the media, or your family?"~Papa Roach

-Diego"What is wrong with the world today, the government, the media, or your family?"~Papa Roach
in C++, structs are exactly the same as classes, except that the default visibility is public in structs, and private in classes

This topic is closed to new replies.

Advertisement