开发者

Can we create object to interfaces and abstract classes?

f.addMouseMotionListen开发者_运维知识库er(new MouseAdapter() {
    public void mouseDragged(MouseEvent e) 
    {
        String s="Mouse dragging :X = "+e.getX()+" Y= "+e.getY();
        tf.setText(s);
    }
    });

i read that we cannot instantiate objects for Abstract classes...but here we are creating new MouseAdapter() ..can someone explain how is it done with these anonymous classes..thank u.


You're not creating an instance of MouseAdapter, you are creating an instance of an anonymous class inheriting from MouseAdapter.

More verbosely, this could have been written:

class Goo extends MouseAdapter { public void mouseDragged(){...} };

f.addMouseMotionListener(new Goo());


You don't see it doesn't mean it won't happen. Just from the code, you use new MouseAdapter() to construct an object. However, if you try to read the content in .class file, you will find that the inner class has name, the usual case is,

class MouseAdapter$1 extends MouseAdapter

Till this, you should clear about everything. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜