Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Dec 2002
    Location
    the windy city
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Code:
    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.

  • #3
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    Dec 2002
    Location
    the windy city
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how about this tutorial from sun?
    http://java.sun.com/docs/books/tutor...nterfaces.html

  • #5
    Regular Coder
    Join Date
    Apr 2003
    Location
    Canada, Ontario, Mississauga
    Posts
    312
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Double V

    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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •