[noob]Doubt regarding Interfaces

Discussion in 'Java' started by mmu2643@gmail.com, Nov 7, 2005.

  1. Guest

    Can someone please explain the following code snippet I found on the
    Swing tutorial site. Thanks!

    Runnable doHelloWorld = new Runnable() {
    public void run() {
    System.out.println("Hello World on " +
    Thread.currentThread());
    }
    };

    Is doHelloWorld now an object or is it an abstract class or is it
    something altogether different?
     
    , Nov 7, 2005
    #1
    1. Advertising

  2. VisionSet Guest

    <> wrote in message
    news:...
    > Can someone please explain the following code snippet I found on the
    > Swing tutorial site. Thanks!
    >
    > Runnable doHelloWorld = new Runnable() {
    > public void run() {
    > System.out.println("Hello World on " +
    > Thread.currentThread());
    > }
    > };
    >
    > Is doHelloWorld now an object or is it an abstract class or is it
    > something altogether different?


    All variables are either primitives or a reference to an Object.
    If they are a reference to an Object as in the case above then they can have
    a type of 1 or many.
    In the example above doHelloWorld is a reference to a concrete Object with
    an interface Type of Runnable.
    It has been created using an Annoymous class, since there is no class
    declaration. If the class was not annoymous then the Object would also have
    the Type of that class.

    Runnable // the Type you are assigning
    doHelloWorld // the variable you are assigning it to
    = // assignment
    new // instantiation
    Runnable() {...} // annoymous class definition
    .... // contents of class

    this bit :
    Runnable() {...} // annoymous class definition

    you can think of expanding to:

    private class AnnoymousClass implements Runnable {
    ....
    }

    HTH
    --
    Mike W
     
    VisionSet, Nov 7, 2005
    #2
    1. Advertising

  3. VisionSet Guest

    "VisionSet" <> wrote in message
    news:pRRbf.5683$...
    >
    > If they are a reference to an Object as in the case above then they can

    have
    > a type of 1 or many.


    If they are a reference to an Object as in the case above then they can have
    1 or many types.
    Yes, all at the same time.
    Google "run time type identification" and "polymorphism"

    --
    Mike W.
     
    VisionSet, Nov 8, 2005
    #3
  4. Roedy Green Guest

    On 7 Nov 2005 15:46:49 -0800, wrote, quoted or
    indirectly quoted someone who said :

    > Runnable doHelloWorld = new Runnable() {
    > public void run() {
    > System.out.println("Hello World on " +
    >Thread.currentThread());
    > }
    > };
    >
    >Is doHelloWorld now an object or is it an abstract class or is it
    >something altogether different?


    it is an instance of an inner anonymous class. see
    http://mindprod.com/jgloss/anonymousclasses.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Nov 8, 2005
    #4
  5. VisionSet wrote:
    > <> wrote in message
    > news:...
    >
    >>Can someone please explain the following code snippet I found on the
    >>Swing tutorial site. Thanks!
    >>
    >> Runnable doHelloWorld = new Runnable() {
    >> public void run() {
    >> System.out.println("Hello World on " +
    >>Thread.currentThread());
    >> }
    >> };
    >>
    >>Is doHelloWorld now an object or is it an abstract class or is it
    >>something altogether different?

    >
    >
    > All variables are either primitives or a reference to an Object.
    > If they are a reference to an Object as in the case above then they can have
    > a type of 1 or many.


    I'm not sure what you mean by that, but perhaps it's that the class of
    an object can be compatible with any positive number of types. It is
    necessary here to be clear about the difference between class and type.
    Every object has exactly one class. Every variable and expression has
    exactly one type. A non-primitive types is expressed in terms of class
    names, and it serves the purpose of describing to the compiler the
    possible classes of the object that its value (a reference) refers to.

    > In the example above doHelloWorld is a reference to a concrete Object with
    > an interface Type of Runnable.


    More precisely, doHelloWorld is a variable of type Runnable, which as
    the result of the assignment holds a reference to an object whose class
    implements Runnable and has Object as its direct superclass.

    > It has been created using an Annoymous class, since there is no class
    > declaration.


    Quibbling a bit here, but there *is* a class declaration -- an anonymous
    one. The syntax is a bit different from that for a named class
    declaration, but that doesn't make it any less a declaration.

    > If the class was not annoymous then the Object would also have
    > the Type of that class.


    Sorry, but that's nonsense. In the first place, Object is a superclass
    of the anonymous class, therefore references to its instances are
    assignment-compatible with type Object. *Every* class has Object as a
    superclass, and therefore *every* reference is assignment-compatible
    with type Object. In the second place, classes do not have types. They
    have superclasses, and they have interfaces implemented; these determine
    which types their instances are compatible with. They do not have types.

    > Runnable // the Type you are assigning

    Runnable // the type of ...
    > doHelloWorld // the variable you are assigning it to

    doHelloWorld // the variable you are assigning a value to
    > = // assignment
    > new // instantiation
    > Runnable() {...} // annoymous class definition


    Which one could also call an anonymous class declaration.

    [...]

    > this bit :
    > Runnable() {...} // annoymous class definition
    >
    > you can think of expanding to:
    >
    > private class AnnoymousClass implements Runnable {
    > ...
    > }


    Yes, that's quite right. More right, in fact, than the idea that the
    class in question really has no name. The Java source code assigns no
    name to it, but the Java compiler invents one at compile time. The VM
    doesn't have any concept of a class that doesn't have a name; "anonymous
    class" really means something more like "class whose name I don't know
    and don't care about."

    --
    John Bollinger
     
    John C. Bollinger, Nov 8, 2005
    #5
  6. Guest

    Re: Doubt regarding Interfaces

    Thanks everyone for your replies!

    I have a basic question - in the above instance Runnable is an
    interface and interfaces cannot be instantiated. So something like 'new
    Runnable()' seems puzzling to me. Is this syntax for instantiating an
    object of the Anonymous class?

    Also, is the above snippet similar to the following in the end result:

    class ac implements Runnable{
    public void run() {
    System.out.println("Hello World on " +
    Thread.currentThread());
    }
    };

    ac doHelloWorld = new ac();
     
    , Nov 8, 2005
    #6
  7. zero Guest

    Re: Doubt regarding Interfaces

    wrote in news:1131466583.776932.254490
    @g47g2000cwa.googlegroups.com:

    > Thanks everyone for your replies!
    >
    > I have a basic question - in the above instance Runnable is an
    > interface and interfaces cannot be instantiated. So something like 'new
    > Runnable()' seems puzzling to me. Is this syntax for instantiating an
    > object of the Anonymous class?
    >
    > Also, is the above snippet similar to the following in the end result:
    >
    > class ac implements Runnable{
    > public void run() {
    > System.out.println("Hello World on " +
    > Thread.currentThread());
    > }
    > };
    >
    > ac doHelloWorld = new ac();
    >
    >


    That's basically it yes.

    new Runnable() {...} creates a new anonymous class that implements
    interface Runnable and has whatever variables/methods you define between
    the brackets.

    This is often used to implement a simple event listener, for example for a
    button:

    Button b = new Button();

    b.addActionListener(new ActionListener()
    {
    JOptionFrame.showMessageDialog("button clicked");
    });

    Note how the whole class definition is inside the parentheses of the
    addActionListener method, and it is ended - like any statement - with a
    semi-colon.
     
    zero, Nov 8, 2005
    #7
  8. Re: Doubt regarding Interfaces

    zero wrote:
    >
    > b.addActionListener(new ActionListener()
    > {

    public void actionPerformed(ActionEvent event) {
    > JOptionFrame.showMessageDialog("button clicked");

    }
    > });
    >
    > Note how the whole class definition is inside the parentheses of the
    > addActionListener method, and it is ended - like any statement - with a
    > semi-colon.


    Well, the class ends with the closing }. The enclosing expression
    carries on as normal.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Nov 8, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    659
  2. Ricewind

    Noob doubt about components

    Ricewind, Jan 28, 2010, in forum: VHDL
    Replies:
    3
    Views:
    5,697
    joris
    Jan 29, 2010
  3. Replies:
    0
    Views:
    592
  4. Peter Otten
    Replies:
    2
    Views:
    137
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    134
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page