Overriding a class as soon as it is initialised
Below code is overriding navigationClick in object GenObject. Is there any other way of overriding navigationClick other than extending GenObject and implementing a method override in sub - class ?
Is there a name for such a construct as below, where an override occurs when the class is initialised ?
GenObject go= new GenObject(){
public boolean navigationClick(int status, int time)
{开发者_如何学JAVA
No, in order to override a method you always have to create a subclass.
What is done in your example code is called an anonymous class, but it's really just a shortcut syntax for creating a subclass. At the bytecode level, it's a class like any other, and it will have its own .class file named something like ContainingClass$0.class
You can only override by subclassing. What you're doing is creating an anonymous
class.
精彩评论