Re: creating objects??
And thus spoke raj_indian_sikh...
import java.awt.event.*;
import javax.swing.*;
public class demo extends JPanel
implements
ActionListener {
public static void main(String[] args) {
JPanel cb2 = new demo();
}//main
}//end class
this is working .why?
Let's ssume you just left out the whole implementation of demo to make
the example shorter.
demo is JPanel.OK but it is also
implementing ActionListener.then how can we
write JPanel on right hand side of cb2.
Demo is a JPanel. And Demo is a ActionListener. If you make...
JPanel cb2 = new demo();
....you can do that, because demo IS a JPanel. You could also do...
ActionListener l = new demo();
....without problems. In this cases, you only take ONE aspect of demo.
The only real difference is, that when using the first statement (cb2)
you can only access the JPanel methods from the outside (because Java
only "knows" that it's a JPanel, but not, that it's also a
ActionListener). So you can, for example, do...
cb2.add(new JLabel("Hello World"));
....but you can NOT do...
new JPanel().addActionListener(cb2);
....because you told Java, that your demo is "only" a JPanel.
If you write...
demo d = new demo();
....you could do both...
d.add(new JLabel("Hello World"));
AND
new JPanel().addActionListener(d);
Flo