Sign in to follow this  

[java] Java as C/C++

This topic is 4357 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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,

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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"?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites

This topic is 4357 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this