一般认为抽象类就是实现了接口的部分方法,但是可不可以全部实现呢?
最近我看到jdk中的WindowAdapter是抽象类,
但是jdk上显示WindowAdapter没有从接口中继承来的类,那表示它实现了接口的所有方法
还有一个实现了接口部分方法的例子,jdk中的抽象类AbstractListModel从ListModel接口继承了
getElementAt, getSize,那表示这两个方法没有实现,需要AbstractListModel的子类去实现请教高手抽象类到底是怎么回事?
最近我看到jdk中的WindowAdapter是抽象类,
但是jdk上显示WindowAdapter没有从接口中继承来的类,那表示它实现了接口的所有方法
还有一个实现了接口部分方法的例子,jdk中的抽象类AbstractListModel从ListModel接口继承了
getElementAt, getSize,那表示这两个方法没有实现,需要AbstractListModel的子类去实现请教高手抽象类到底是怎么回事?
其实抽象类和接口功能是基本相同的,因为java类是不允许继承多个超类的,所以就出现了接口。
如果继承WindowListener接口就要实现它的全部方法,但是有时候你可能只需要响应windowClosing方法,而忽略其他方法的响应,这时候你就只需继承WindowAdapter,复写windowClosing,WindowAdapter类实现了WindowListener接口,但是方法体全部为空,也就是对这些事件不作任何响应,你只需要复写需要的方法就可以了,你可以看一下它的源代码
----------------
public abstract class WindowAdapter
implements WindowListener, WindowStateListener, WindowFocusListener
{
/**
* Invoked when a window has been opened.
*/
public void windowOpened(WindowEvent e) {} /**
* Invoked when a window is in the process of being closed.
* The close operation can be overridden at this point.
*/
public void windowClosing(WindowEvent e) {} /**
* Invoked when a window has been closed.
*/
public void windowClosed(WindowEvent e) {} /**
* Invoked when a window is iconified.
*/
public void windowIconified(WindowEvent e) {} /**
* Invoked when a window is de-iconified.
*/
public void windowDeiconified(WindowEvent e) {} /**
* Invoked when a window is activated.
*/
public void windowActivated(WindowEvent e) {} /**
* Invoked when a window is de-activated.
*/
public void windowDeactivated(WindowEvent e) {} /**
* Invoked when a window state is changed.
* @since 1.4
*/
public void windowStateChanged(WindowEvent e) {} /**
* Invoked when the Window is set to be the focused Window, which means
* that the Window, or one of its subcomponents, will receive keyboard
* events.
*
* @since 1.4
*/
public void windowGainedFocus(WindowEvent e) {} /**
* Invoked when the Window is no longer the focused Window, which means
* that keyboard events will no longer be delivered to the Window or any of
* its subcomponents.
*
* @since 1.4
*/
public void windowLostFocus(WindowEvent e) {}
}
abstract class A{
public void getValue(){}
}
肯定是可以的。有抽象方法类必为抽象,反过来抽象类就不一定有抽象方法了!