开发者

abstract class MouseAdapter vs. interface

I noted this (it's a java.awt.event class).

public abstract class MouseAdapter implements MouseListener, 
                                              MouseWheelListener, 
                                              MouseMotionListener {
....
}

Then you are clearly forced to extend from this adapter

public class MouseAdapterImpl extends MouseAdapter {}

the class is abstract and implements no methods. Is this a strategy to combine different interfaces into a single "basically interface" ? I assume in java it's not possible to combine different interfaces into a single one without using this approach.

In other words, it's not possible to do something like this in java

public interface MouseAdapterIface extends MouseListener, 
                                        开发者_如何学运维   MouseWheelListener, 
                                           MouseMotionListener {
}

and then eventually

public class MouseAdapterImpl implements MouseAdapterIface {}

Is my understanding of the point correct ? what about C# ?


In other words, it's not possible to do something like this in java

public interface MouseAdapterIface extends MouseListener, 
                                           MouseWheelListener, 
                                           MouseMotionListener {
}

Sure, you can do that in Java.

The reason for having an abstract class is that it also provides default implementations for all methods (which do nothing), so that you only have to implement those you are interested in.

the class is abstract and implements no methods

Not true. It is abstract, but it does implement all methods in the three interfaces.


In Dotnet, you can inherit your interface from n other interfaces but not from any abstract class. If MouseListener,MouseWheelListener,MouseMotionListener are interfaces then dotnet allows to inherit from it.

This is not allowed in DotNet


public abstract class AbstractA
{
  public abstract void A();
}

public interface IA:AbstractA
{
  void A();
}

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜