Anonymous classes must extend or implement something, like any other Java class, even if it's just java.lang.Object.
For example:
Runnable r = new Runnable() {
public void run() { ... }
};
Here, r is an object of an anonymous class which implements Runnable.
An anonymous class can extend another class using the same syntax:
SomeClass x = new SomeClass() {
...
};
What you can't do is implement more than one interface. You need a named class to do that. Neither an anonymous inner class, nor a named class, however, can extend more than one class.
Answer from skaffman on Stack OverflowAre there any limitations when using anonymous classes with Java's functional interfaces?
Can an anonymous class access private members of the enclosing class?
How can I manage exceptions effectively within an anonymous class?
Anonymous classes must extend or implement something, like any other Java class, even if it's just java.lang.Object.
For example:
Runnable r = new Runnable() {
public void run() { ... }
};
Here, r is an object of an anonymous class which implements Runnable.
An anonymous class can extend another class using the same syntax:
SomeClass x = new SomeClass() {
...
};
What you can't do is implement more than one interface. You need a named class to do that. Neither an anonymous inner class, nor a named class, however, can extend more than one class.
An anonymous class usually implements an interface:
new Runnable() { // implements Runnable!
public void run() {}
}
JFrame.addWindowListener( new WindowAdapter() { // extends class
} );
If you mean whether you can implement 2 or more interfaces, than I think that's not possible. You can then make a private interface which combines the two. Though I cannot easily imagine why you would want an anonymous class to have that:
public class MyClass {
private interface MyInterface extends Runnable, WindowListener {
}
Runnable r = new MyInterface() {
// your anonymous class which implements 2 interaces
}
}
An anonymous class can either implement exactly one interface or extend one class.
One workaround is to create a named class that extends Base and implements Interface1, and then use that as the base class for the anonymous class:
public abstract class Base1 extends Base implements Interface1 {}
...
protected Interface1 varClass1 = new Base1() {
...
While it is not possible to both extend a class and implement an interface, what you probably what to do can be achieved using delegation.
Just create an instance of Base in the initializer of your anonymous class and use its methods from the methods you implement from Interface1.
EDIT: My answer assumes that you only want to inherit from Base because of its implementation, not because of its interface. This is a valid assumption given that the interface of Baseis not visible after assigning the object to a Interface1 variable. Therefore, I think it's better to use delegation here instead of creating a new class.
No, you can't do that with an anonymous class. You can create a named class within a method if you really want to though:
class Base {
}
interface Interface {
}
public class Test {
public static void main(String[] args) {
class Foo extends Base implements Interface {
};
Foo x = new Foo();
}
}
Personally I'd usually pull this out into a static nested class myself, but it's your choice...
This scenario makes little sense to me. Here's why: assume class Base is this:
class Base {
public void foo();
}
and Interface is this:
interface Interface {
public void bar();
}
Now you want to create an anonymous class that would be like this:
class MyClass extends Base implements Interface {
}
this would mean that MyClass inherits (or possibly overrides) method bar from Base and must implement method foo from Interface. So far so good, your class has these two methods either way.
Now, think what happens when you are returning an object of type Interface from your method: you only get the functionality that Interface offers, namely method foo. bar is lost (inaccessible). This brings us down to two cases:
- Extending
Baseis useless because you do not get to use its methods, since the new object is seen as anInterface. Interfacealso has a methodfoo, but that would mean thatBaseitself should implementInterface, so you can just extendBasedirectly:
class Base implements Interface { public void foo(); public void bar(); } class MyClass extends Base { }
From JLS 15.9.5:
An anonymous class is always implicitly final (§8.1.1.2).
In the interest of "teach a person to fish:" I just went to the index page for the JLS, searched for "anonymous class" to find 15.9.5, clicked through, and the answer was right there at the start of the section.
You can extend any class dynamically using a Proxy so essentially yes - if the anonymous calss implements an interface.