Can an anonymous class be declared as implementing an interface and extending a class?
An anonymous class may implement an interface or extend a superclass, but may not be declared to do both.
An anonymous class is a local class without a name. An anonymous class is defined and instantiated in a single succinct expression using the new operator.
new class-name ( [ argument-list ] ) { class-body }
or:
new interface-name () { class-body }
public class InitializerDemo
{
// This is an instance variable.
public int[] array1;
// This is an instance initializer. It is an arbitrary block of code.
// It runs for every new instance, after the superclass constructor
// and before the class constructor, if any. It can serve the same
// function as a constructor with no arguments.
{
array1 = new int[10];
for(int i = 0; i < 10; i++) array1[i] = i;
}
// The line below contains another instance initializer. The instance
// initializers for an object are run in the order in which they appear
// in the class definition.
int[] array2 = new int[10]; { for(int i=0; i<10; i++) array2[i] = i*2; }
static int[] static_array = new int[10];
// By contrast, the block below is a static initializer. Note the static
// keyword. It runs only once, when the class is first loaded.
static {
for(int i = 0; i < 10; i++) static_array[i] = i;
}
}
Recent Comments