• Advertisement
Sign in to follow this  

Implementation

This topic is 2470 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

I'm using a book to learn Java, and I'm on the part about Interfaces. It keeps saying that in an interface, you have everything a class has, except that your variables and methods are not implemented. I'm just not too sure what this means,
Any help is really appreciated
Thanks
Peter

Share this post


Link to post
Share on other sites
Advertisement

I'm using a book to learn Java, and I'm on the part about Interfaces. It keeps saying that in an interface, you have everything a class has, except that your variables and methods are not implemented. I'm just not too sure what this means,
Any help is really appreciated
Thanks
Peter



It means that you dont actually write the code inside of the methods. You create the signature such as "public void doSomething();" but you dont provide anything for it to do. The purpose is that any class that implements the interface will be forced to actually use the method and define its own version of it.

Share this post


Link to post
Share on other sites

I'm using a book to learn Java, and I'm on the part about Interfaces. It keeps saying that in an interface, you have everything a class has, except that your variables and methods are not implemented. I'm just not too sure what this means,
Any help is really appreciated
Thanks
Peter


An interface gives you a template for a class, with the objects the class will contain, but doesn't fill in the values. Example, if you create an interface for a Character. You know the character will have a draw and update method and a health variable, but you will have to define how each of these will work from within the class (ie Main Character, Enemy Character).

It's useful because if you want to do something to all characters (say draw them on a screen or take away health), and you want to make sure that they have that functionality, you can be sure that if it's in the interface, it's in the final class. You just won't know the details of how it's done unless you look directly at the class.

Share this post


Link to post
Share on other sites

I'm using a book to learn Java, and I'm on the part about Interfaces. It keeps saying that in an interface, you have everything a class has, except that your variables and methods are not implemented. I'm just not too sure what this means,
Any help is really appreciated
Thanks
Peter


basically an interface would be:

public interface myInterface {
public someMethod();
public someOtherMethod();
public publicVar;
}

while a class using that interface would be

public class myClass implements myInterface {
public someMethod() {
code
}
etc
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement