Interface problem

Hi everyone,

I supposed that we can not instantiate interfaces. But the below code refute this.

So why ?

Would you please explain the logic of the following ?

Runnable updateAComponent =new Runnable(){

publicvoid run(){ component.doSomething();}

};

SwingUtilities.invokeLater(updateAComponent);

[551 byte] By [samue-1a] at [2007-10-2 18:18:59]
# 1

Another example is that :

ActionListener actionPrinter = new ActionListener() {

public void actionPerformed(ActionEvent e) {

try { pane.getStyledDocument().insertString(0 ,

"Action ["+e.getActionCommand()+"] performed!\n", null);

} catch (Exception ex) { ex.printStackTrace(); }

}

};

samue-1a at 2007-7-13 19:39:25 > top of Java-index,Java Essentials,Java Programming...
# 2
You're not instantiating an interface. You're instantiating a class (an unnamed, anonymous one) which implements the interface.
warnerjaa at 2007-7-13 19:39:25 > top of Java-index,Java Essentials,Java Programming...
# 3
> You're not instantiating an interface. You're> instantiating a class (an unnamed, anonymous one)> which implements the interface.How do you know that this class implements the interface ?
samue-1a at 2007-7-13 19:39:25 > top of Java-index,Java Essentials,Java Programming...
# 4
> How do you know that this class implements the> interface ?It's written in your code.
da.futta at 2007-7-13 19:39:25 > top of Java-index,Java Essentials,Java Programming...
# 5
If you want to know more, google for java anonymous inner class.
jverda at 2007-7-13 19:39:25 > top of Java-index,Java Essentials,Java Programming...
# 6

> > You're not instantiating an interface. You're

> > instantiating a class (an unnamed, anonymous one)

> > which implements the interface.

>

> How do you know that this class implements the

> interface ?

If its type is declared to be that of the interface, and it implements that interface's methods, then it implements the interface. If it didn't, it wouldn't compile.

jverda at 2007-7-13 19:39:25 > top of Java-index,Java Essentials,Java Programming...
# 7
Thanks for your all replies...And, I think that using an Anonymous inner class is very useful since it provides time efficiency since it is easy to write.What do you think about that ?Do you all agree with me?Thanks,Mert
samue-1a at 2007-7-13 19:39:25 > top of Java-index,Java Essentials,Java Programming...
# 8

> And, I think that using an Anonymous inner class is

> very useful since it provides time efficiency since

> it is easy to write.

> What do you think about that ?

I think it's very shortsighted, because the time you save to write it can easily be eaten up by refactoring work or slower maintenance because of unclear code that results from bad design.

> Do you all agree with me?

It's a by-case decision whether an anonymous inner class is appropriate.

CeciNEstPasUnProgrammeura at 2007-7-13 19:39:25 > top of Java-index,Java Essentials,Java Programming...
# 9

> And, I think that using an Anonymous inner class is very useful since

> it provides time efficiency since it is easy to write.

I'd rather call it convenient for the programmer; no more no less.

But then again, I'm still waiting for that one super duper framework

which exposes just one method: "doItAll()". Now *that's* what I call

convenience ;-)

kind regards,

Jos

JosAHa at 2007-7-13 19:39:25 > top of Java-index,Java Essentials,Java Programming...
# 10
it may provide time efficiency but it's a ***** to debug if something goes wrong.J
jagulara at 2007-7-13 19:39:25 > top of Java-index,Java Essentials,Java Programming...
# 11
> it may provide time efficiency but it's a ***** to> debug if something goes wrong.> > JWhy ?I think it is not...
samue-1a at 2007-7-13 19:39:25 > top of Java-index,Java Essentials,Java Programming...
# 12
> I think it is not...Only if it's you who also wrote the class and if you can actually remember your design rationales. Can look differently if you read someone elses code.
CeciNEstPasUnProgrammeura at 2007-7-13 19:39:26 > top of Java-index,Java Essentials,Java Programming...
# 13
> > I think it is not...> > Only if it's you who also wrote the class and if you> can actually remember your design rationales. Can> look differently if you read someone elses code.Hmm, maybe it is...
samue-1a at 2007-7-13 19:39:26 > top of Java-index,Java Essentials,Java Programming...
# 14
> which exposes just one method: "doItAll()". Now> *that's* what I callBeanShell.exec( ) ?
mlka at 2007-7-13 19:39:26 > top of Java-index,Java Essentials,Java Programming...
# 15

> But then again, I'm still waiting for that one super

> duper framework

> which exposes just one method: "doItAll()". Now

> *that's* what I call

> convenience ;-)

>

> kind regards,

>

> Jos

Oh yeah, that's really a good thing to have. I've written such a doItAll(), it works great. It's in my NOOP class.

Yours truly,

df.

da.futta at 2007-7-20 23:39:38 > top of Java-index,Java Essentials,Java Programming...
# 16
> > which exposes just one method: "doItAll()". Now *that's* what I call> > BeanShell.exec( ) ?Without arguments? Nah? 't won't work I'm telling ya.kind regards,Jos
JosAHa at 2007-7-20 23:39:38 > top of Java-index,Java Essentials,Java Programming...
# 17
> Without arguments? Nah? 't won't work I'm telling ya.If it's "doing it all", what on earth do you need arguments for?;o)
yawmarka at 2007-7-20 23:39:38 > top of Java-index,Java Essentials,Java Programming...
# 18
Lovely forum software. Urgh.
yawmarka at 2007-7-20 23:39:38 > top of Java-index,Java Essentials,Java Programming...
# 19
> Lovely forum software. Urgh.Did something get goofed up in your posts here?(confused)
warnerjaa at 2007-7-20 23:39:38 > top of Java-index,Java Essentials,Java Programming...
# 20

> > But then again, I'm still waiting for that one super duper framework

> > which exposes just one method: "doItAll()".

> Oh yeah, that's really a good thing to have. I've written such a doItAll(),

> it works great. It's in my NOOP class.

Just because I'm psychic I happen to know that you are bluffing :-P

And btw, a proper NOOP class doesn't have methods nor fields,

otherwise it should've been named AlmostNOOP; and if it had contained

a real full blown 'doItAll' method it should've been named

AbsolutelyNONOOP. So there.

kind regards,

Jos

JosAHa at 2007-7-20 23:39:38 > top of Java-index,Java Essentials,Java Programming...
# 21

> Did something get goofed up in your posts here?

> (confused)

Double-post.

Okey nothing happend

Stop talking about that

-- Raddadi, "[url=http://forum.java.sun.com/thread.jsp?thread=512391&forum=31&start=10&range=1]Reply #10"[/url]

yawmarka at 2007-7-20 23:39:38 > top of Java-index,Java Essentials,Java Programming...
# 22

> > Did something get goofed up in your posts here?

> > (confused)

>

> Double-post.

>

> Okey nothing happend

> Stop talking about that

> -- Raddadi,

> "[url=http://forum.java.sun.com/thread.jsp?thread=5123

> 91&forum=31&start=10&range=1]Reply #10"[/url]

I see no double-post here, so you must be right "Okey nothing happend" ;-)

warnerjaa at 2007-7-20 23:39:38 > top of Java-index,Java Essentials,Java Programming...
# 23

> > Oh yeah, that's really a good thing to have. I've

> written such a doItAll(),

> > it works great. It's in my NOOP class.

>

> Just because I'm psychic I happen to know that you

> are bluffing :-P

Just because I'm empathic I happen to konw that before you put your british motorbike of a psychic capacity into action, you had already guessed I were bluffing, so you should have said "Almost just because I'm psychic blablabla drooldrool".

And if you really were as slow as you pretend to be, a superb display of inertia, your post should have been AbsolutelyNoPostAtAll, not yet at any rate.

So there.

> And btw, a proper NOOP class doesn't have methods nor

> fields,

> otherwise it should've been named AlmostNOOP; and if

> it had contained

> a real full blown 'doItAll' method it should've been

> named

> AbsolutelyNONOOP. So there.

Let's ask UJ. I guess that's a good application of the "Liskov Subsitution Substitution Principle", you know, "the hand is faster than the eye", nudge nudge?

I'm so happy I'll just do it and put ™ everywhere.

Yours truly,

df. (← so there!)

da.futta at 2007-7-20 23:39:38 > top of Java-index,Java Essentials,Java Programming...