Helpful Information
 
 
Category: Java and JSP
what is impelements (Java)

Can someone tell me what is implements mean??

import java.awt.*;
import java.awt.event.*;
public class MyClass extends JFrame implements ActionListener{

I understand what extends mean (super class and sub class), but what is implements mean?

Also, I am new with Java, when explaining, pleae keep it simple... example will be great.

Mike
:)

Mike,
it's hard for me to explain this in just a couple of sentences.
"implements" is a keyword that deals with interfaces.
you can look at interfaces as java's way around multiple inheritance (which, as you probably know, java doesn't have). interfaces are a huge topic of their own.
think of this example (not exactly "real world", but should get the point across): you need to define a class Orange. Orange is a fruit, so you think that class Orange should be a child of class Fruit (or may be class Fruit is devided into more sub-classes, and Orange would fall into class Citrus. those details are not as important right now). let's imagine that you wanted to define some behaviour of an orange (for example, how it would behave when you threw it, or bounced off the wall ;) ). orange has a spherical shape, but not every fruit does. this means that class Orange needs to have some kind of behavior that is not common to every fruit, but is common among certain type of fruit (like, citrus, for example). to ensure that all citrus fruit is capable of performing the same behaviour, you write an interface that will define what a class should do. then, when you implement an interface within a class, you actually write how each particular behaviour will be for this class.
in short, "an interface defines what, but not how". the "how" will be defined within a class that implements an interface. inteface is a set of requirements. those requirements look like empty methods.

public interface CitrusBehaviour {
void bounceoffTheWall(int speed, int angle, int weight);
void flyWhenThrown(int speed, int angle, int weight);
}
any class that wants to implement this interfacemust have bounceoffTheWall and flyWhenThrown methods, and those methods must take the parameters specified in an interface. (methods in an interface are automatically public, so you don't have to specify that separately). of course, when those methods will be defined in classes, they shoudl do whatever they are "meant" to do, i.e. bounceoffTheWall method should provide taht behaviour. etc.

interfaces are used the most for callbacks (that's what is in your example), but the topic is really huge. i'll write something up tonight.

implements is basically saying that it is using the "actionlistener" class as part of its operations. So now you can find mouse click and use other methods that are part of the actionlistener class


Jason

how about this tutorial from sun?
http://java.sun.com/docs/books/tutorial/java/interpack/interfaces.html

Wow wow wow...

You do know you Java, I kinda understand what you are saying but not totally. I guess time and pratice will take care of that.

Anyway, thanx...:thumbsup:










privacy (GDPR)