What is Anonymous inner class?

You can also declare an inner class withen the body of a method without naming it.
Such class are known as anonymous inner class.
class Popcorn {
public void pop() {
class demo {
public static void main(String[] args){
Popcorn p = new Popcorn() {
public void pop() {
System.out.println("anonymous popcorn");}};//Anonymous class definition ends with semicolon
The Popcorn reference variable refers not to an instance of Popcorn, but to an
instance of an anonymous (unnamed) subclass of Popcorn. Declare a reference variable, p, of type Popcorn. Then declare a new class that has no name, but that is a subclass of Popcorn. And here's the curly brace that opens the class definition.
And what is it doing? Overriding the pop() method of the superclass
Popcorn. This is the whole point of making an anonymous inner class—to override
one or more methods of the superclass!
- Polymorphism is in play when anonymous inner classes are involved.
-We can not define a new method in inner class which does not exist in the outer class.
- In the preceding Popcorn example, we're using a superclass reference
variable type to refer to a subclass object. What are the implications? You can
only call methods on an anonymous inner class reference that are defined in the
reference variable type! This is no different from any other polymorphic references,
for example,
class Horse extends Animal{
void buck() { }
class Animal {
void eat() { }
class Test {
public static void main (String[] args) {
Animal h = new Horse();
h.eat(); // Legal, class Animal has an eat() method
h.buck(); // Not legal! Class Animal doesn't have buck()

if Popcorn were an interface type instead of a class type, then the new
anonymous class would be an implementer of the interface rather than a subclass of
the class. Look at the following example:
interface Cookable {
public void cook();//interface type, no {}
class demo {
Cookable c = new Cookable() {
public void cook() {
System.out.println("anonymous cookable implementer");
- There simply isn't any mechanism to say that
your anonymous inner class is going to implement multiple interfaces.

Don't be fooled by any attempts to instantiate an interface except in the
case of an anonymous inner class. The following is not legal,
Runnable r = new Runnable(); // can't instantiate interface
whereas the following is legal, because it's instantiating an implementer of the
Runnable interface (an anonymous implementation class):
Runnable r = new Runnable() { // curly brace, not semicolon
public void run() { }

Key Points:
❑ Anonymous inner classes have no name, and their type must be either a
subclass of the named type or an implementer of the named interface.
❑ An anonymous inner class is always created as part of a statement; don't
forget to close the statement after the class definition with a curly brace. This
is a rare case in Java, a curly brace followed by a semicolon.
❑ Because of polymorphism, the only methods you can call on an anonymous
inner class reference are those defined in the reference variable class (or
interface), even though the anonymous class is really a subclass or implementer
of the reference variable type.
❑ An anonymous inner class can extend one subclass or implement one
interface. Unlike non-anonymous classes (inner or otherwise), an anonymous
inner class cannot do both. In other words, it cannot both extend a class and
implement an interface, nor can it implement more than one interface.
❑ An argument-defined inner class is declared, defined, and automatically
instantiated as part of a method invocation. The key to remember is that the
class is being defined within a method argument, so the syntax will end the
class definition with a curly brace, followed by a closing parenthesis to end
the method call, followed by a semicolon to end the statement: });

No comments:

Post a Comment