[java] Java as C/C++

Started by
8 comments, last by BlackWind 18 years, 3 months ago
Hi, for those who know c/c++ can remeber than when we create a class it is possible to make the declaration in one file and the implementation in another one: Example: "clase.h"

class myclass
{
 int x;
 void method();
};
implementations in "clase.cpp"

void myclass::method()
{
 x++;
}
And then use that class in another file. The question is: Is it possible to do something like that in Java? if yes.... how? thanks,
Advertisement
There is something similar called Interfaces, where in one class you define all the method signatures that an implementing class must have, and then when a class 'implements' the interface it is agreeing to contain all the methods described (though it can also have its own methods). Also, interfaces cannot contain variables. Should you wish to have the class contain variables, look at abstract classes which are similar but can contain variables, although when you have a class full of abstract methods it is generally best to use an interface instead.

Look here for more info, though I don't think these things will give you the effect you're looking for and to be honest theres not much need for it. You couldn't really import the 'header' files into Java and then use the implementing classes without importing them also, so theres really nothing to be gained. If you want to use c/c++ methodologies then use c/c++, if you want to use Java then use Java style! You should only use interfaces and abstract classes when the situation calls for it, not in every class :)

http://java.sun.com/docs/books/tutorial/java/interpack/createinterface.html
http://java.sun.com/docs/books/tutorial/java/javaOO/abstract.html
Not really and I don't see why you'd want to. With headers, every time you make a interface change you have to make two changes, one in implementation and one in header. In Java you only need make one change.

If you are thinking of using header because it allows easy viewing of the class interface, don't. Use javadocs for that, because it is definately so much better.
Java does not use headers. You just import the class that you want to use, instead of using headers.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]
thanks a lot...

Quote:Original post by Vyper_uk
You should only use interfaces and abstract classes when the situation calls for it, not in every class :)


i read the tutos, but i didnt understand when a situation will call for that...
can you explain it?

And one more thing...
what are "javadocs"?
Quote:Original post by BlackWind
i read the tutos, but i didnt understand when a situation will call for that...
can you explain it?


This happens when you don't want to bother about knowing what the implementation behind an interface is. Examples:

  • You can create a function that works on all objects that satisfy an interface, such as a button interface. No matter what the code for the button object you wrote is, the function will be able to manipulate it. This allows you to create a new button class with your own code, and still be able to add that button to a form that was written before you wrote the button class.

  • You can use RMI: provide an interface on both the calling and called computer. On the calling computer, the interface is implemented by a class that forwards calls to the other computer. On the called computer, the interface is implemented by whatever means you want. This allows you to call functions on objects that are stored on other computers.

  • You can replace the implementation of an object at will. You can replace a doubly linked list implementation by an array implementation if the need arises.


As for javadoc, check the first google result.
Allow me to give another example:

Suppose you have some polygons you want to draw. Now, you could go through and draw all of your squares, triangles, etc individually. That makes for a lot of duplicated code.
The easier way is to create an interface called Polygon that defines a single method called draw(). Then you create classes (Square, Triangle, etc) that implement the Polygon interface, and consequently, the draw() method.
Now you can have an array of Polygons and just call draw() on each Polygon instead of having to worry if it's a Square, Triangle, etc.

Hope that helps!
Javadoc is a way of automatically generating documentation. If you have used doxygen, it will be familiar.

Basically, before each function in the source file, you write a short description. Then the javadoc program crunches through and generates a reference documentation that lets you browse through the structure and interface of your program in a web browser.
Hell, if all you want is a class's public interface, you can grab that at runtime in Java with reflection. Plus, Javadoc, Eclipse both do the same thing for you as well.

Hell, the Java platform even has a tool print out the public interface of an arbitrary .class file. You don't even need the source code....
thanks for your replies, now everything is clearer hehe............

This topic is closed to new replies.

Advertisement