java - How come not implementing some Methods are allowed in WindowAdapter? -
the windowadapter
class of java defined abstract class , has many abstract methods, including:
windowclosing() windowclosed() windowactivated()
all of these methods empty , java says class exists convenience not want create classes implementing windowlistener
. because unlike windowlistener
interface, windowadapter
gives choice implement 1 of abstract methods defined in it.
for example if add below code class inherits window
, make window closeable through 'x' button on upper right corner:
addwindowlistener(new windowadapter() { public void windowclosing(windowevent we) { dispose(); } });
however confuses me. first of happening here? creating inner class extends windowadapter
? new
keyword used create instance, not allowed instantiate abstract class. why new
keyword here?
second, why away implementing 1 of abstract methods in windowadapter
? in java if define abstract class:
public abstract class upperclassab { public abstract void test(); public abstract int boa(); }
and try use class, have used windowadapter
above:
upperclassab tester = new upperclassab() { public void test() { system.out.println("mor"); } };
i error, because not implementing abstract methods 1 of them. how can away implementing 1 of abstract methods in case of windowadapter
? single case, , if not can imitate behavior?
although windowadapter abstract class, methods not abstract empty. not have implement them, can override them if want.
https://docs.oracle.com/javase/7/docs/api/java/awt/event/windowadapter.html
regarding new keyword: called anonymous class. create new class without name, , instantiate @ same time. new class not have name, extends windowadapter, overrides 1 method, , can instantiated @ location (because has no name)
Comments
Post a Comment