blue-sky
blue-sky

Reputation: 53806

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)
            {

Upvotes: 1

Views: 66

Answers (2)

Michael Borgwardt
Michael Borgwardt

Reputation: 346300

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

Upvotes: 4

Johan Sjöberg
Johan Sjöberg

Reputation: 49187

You can only override by subclassing. What you're doing is creating an anonymous class.

Upvotes: 3

Related Questions