Sign in to follow this  
destructivArts

Implementation

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
[quote name='destructivArts' timestamp='1305658703' post='4812032']
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
[/quote]


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
[quote name='destructivArts' timestamp='1305658703' post='4812032']
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
[/quote]

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
[quote name='destructivArts' timestamp='1305658703' post='4812032']
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
[/quote]

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

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