Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

creating objects??

Thread view: 
raj_indian_sikh - 06 Jan 2007 13:38 GMT
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.
Paul Hamaker - 06 Jan 2007 13:42 GMT
demo extends JPanel
demo IS A JPanel

Btw, it's better to start class names with an uppercase : Demo.
--
http://javalessons.com  Paul Hamaker, SEMM
Teaching ICT since 1987
Flo 'Irian' Schaetz - 06 Jan 2007 14:18 GMT
And thus spoke raj_indian_sikh...

> import java.awt.event.*;
> import javax.swing.*;
[quoted text clipped - 10 lines]
> }//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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.