General question about interface


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: General question about interface

  1. #1
    Join Date
    Oct 2005
    Posts
    3

    Question General question about interface

    Hi Guys!
    I have been studying Java for a month by book and its going prety well (until now... ). I just started to learn about interface in general and the MouseListener biside, but I think I didnt get to the bottom yet.
    They wrote an applet for example and I have a question about it: the idie is to make a little square where the mouse is clicked. They made two .java files, one for the Listener and one for the main applet. in the Listener they made a class that extends the MouseAdapter (???).
    My questions are:
    1. Why did they had to use 2 files and couldnt made just one?
    2. what is this MouseAdapter? (I guess its an object, but what is it excactly??)
    3. Does anyone know where can I read more about Interface?

    * I didnt want to write the code lines because its prety long, but I will do it if it helps.
    Thanks!
    Itay.

  2. #2
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    1- its a design issue, require experience with OOP concept
    .
    2- *Adapter classes you will see in java.
    if you implement an interface, you should implement all methods inside..
    but imagine you need only 1 method of interface
    and it has 10 methods.
    so you must to do something like this for 9 methods:

    public void method2(){/*nothing to do*/};

    *Adapter classes does it for you, look inside of them.

  3. #3
    Join Date
    Aug 2005
    Posts
    23
    Quote Originally Posted by maoz3000
    Does anyone know where can I read more about Interface?
    http://java.sun.com/docs/books/tutor...interface.html
    http://mindprod.com/jgloss/interface.html
    http://java.sun.com/docs/books/tutor...interface.html

    --------------
    Naveen Vooka
    www.devsquare.com
    DevSquare - Online Application Development

  4. #4
    Join Date
    Oct 2005
    Posts
    3
    Thank you both for the replies, I used those documents and it really help me but I still got this one question. I will write down two example applets from my study book, even though they are not doing the same thing, I would like to know why the first one is written in one file and the other applet has to be divided into two files.
    here are the applets:

    The first applet (tutorial29.java)
    import java.awt.*;
    import java.awt.event.*;
    public class tutorial29 extends java.applet.Applet implements MouseListener {
    TextField txt;
    int i=0;
    public void init() {
    this.addMouseListener(this);
    txt=new TextField("This is textField", 40);
    add(txt);
    }
    public void mouseClicked(MouseEvent e) {/*some code*/}
    public void mouseEntered(MouseEvent e) {/*some code*/}
    public void mouseExited(MouseEvent e) {/*some code*/}
    public void mousePressed(MouseEvent e) {/*some code*/}
    public void mouseReleased(MouseEvent e) {/*some code*/}
    }
    The second applet: (mListener.java) /*The Listener*/

    import java.awt.*;
    import java.awt.event.*;
    public class mListener extends MouseAdapter {
    tutorial29b aplt;
    mListener(tutorial29b aplt_temp) {
    aplt=aplt_temp;
    }
    public void mouseClicked(MouseEvent e) {
    aplt.x=e.getX();
    aplt.y=e.getY();
    aplt.repaint();
    }
    }
    and his other file: (tutorial29b.java) /*the main Applet*/

    import java.awt.*;
    import java.awt.event.*;
    public class tutorial29b extends java.applet.Applet {
    mListener t;
    int i=0,x=0,y=0;
    public void init() {
    t=new mListener(this);
    this.addMouseListener(t);
    }
    public void paint(Graphics g) {
    g.drawRect(x,y,100,100);
    }
    }

    I do understand that if I implement an interface I must Implement all of his methods, even if I dont inted to use them, also I understand that the mListener which I created is inherited from the MouseAdapter object which already contains the empty methods. But still that doesnt gives me the answer why do I have to devide my applet into 2 files the other way.
    thank you for your help, again!
    I really appreciate that
    Itay.

  5. #5
    Join Date
    Sep 2005
    Location
    istanbul / Turkey
    Posts
    133
    Quote Originally Posted by maoz3000
    ...
    But still that doesnt gives me the answer why do I have to devide my applet into 2 files the other way.
    thank you for your help, again!
    I really appreciate that
    Itay.
    to devide is not a "must" for working-code.
    its a design issue.
    ofcourse , you can put all of your code into 1 class.
    but this class may be very long, very un-readable..
    so , testing it and finding bugs will be hard.

    when you feel things are getting complicated ,
    you may think to define a new class
    and give some of reponsibility( work ) to it.
    this will help code-reuse too.

    there may be a lot of reasons to do it .
    if you want to search , reading about "design patterns" will help. but this may be too hard for beginners.
    maybe others can advice some good books.

    happy coding.

Similar Threads

  1. General job search question
    By Bill in forum Careers
    Replies: 9
    Last Post: 04-18-2002, 05:15 PM
  2. Replies: 3
    Last Post: 06-18-2001, 12:35 PM
  3. Interface question
    By Micki in forum .NET
    Replies: 2
    Last Post: 05-14-2001, 09:52 AM
  4. General DB Question
    By Aravind in forum VB Classic
    Replies: 1
    Last Post: 05-02-2001, 04:29 PM
  5. Interface leaks in C#?
    By Kevin Burton in forum .NET
    Replies: 12
    Last Post: 10-09-2000, 10:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center