creating objects??

  • Thread starter raj_indian_sikh
  • Start date
R

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?
demo is JPanel.OK but it is also

implementing ActionListener.then how can we

write JPanel on right hand side of cb2.
 
P

Paul Hamaker

demo extends JPanel
demo IS A JPanel

Btw, it's better to start class names with an uppercase : Demo.
 
F

Flo 'Irian' Schaetz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top