General question about interface
Hi Guys!
I have been studying Java for a month by book and its going prety well (until now... :confused: ). 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.
[874 byte] By [
maoz3000] at [2007-11-11 6:59:19]

# 1 Re: General question about interface
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.
mr1yh1 at 2007-11-11 22:39:48 >

# 2 Re: General question about interface
Does anyone know where can I read more about Interface?
http://java.sun.com/docs/books/tutorial/java/concepts/interface.html
http://mindprod.com/jgloss/interface.html
http://java.sun.com/docs/books/tutorial/java/interpack/createinterface.html
-----
Naveen Vooka
www.devsquare.com
DevSquare - Online Application Development
# 3 Re: General question about interface
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.
# 4 Re: General question about interface
...
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.
mr1yh1 at 2007-11-11 22:42:50 >
