Anonymous inner class features:
1. Anonymous inner classes are internal classes that do not have a name and cannot be referenced. Instances of these must be declared and created as part of the new statement at the time of creation.
2. Anonymous inner classes must inherit a class (abstract, non-abstract) or implement an interface. If the parent class (or parent interface) is an abstract class, the anonymous abstract class
All of its abstract methods must be implemented.
3. Anonymous inner classes can define code blocks for instance initialization, but cannot define static blocks of code.
Anonymous INNER class statement:
New Interface/superclass () {//Class body}
This form of the new statement declares a novel anonymous class that expands on a given class, or implements a given interface, and simultaneously creates a new one for that anonymous class.
Instance.
public class Anonymousinnerclass {public static void main (string[] args) {//TODO auto-generated method Stubperson p = new Person ();/*animal dog = new Dog ();p. Feed (dog); *///Anonymous class New Animal () {///anonymous class can have its own properties and methods, but it cannot be instantiated because the parent class is instantiated, The parent class does not change the properties and methods in the anonymous class private String name = "AAA"; The {///anonymous class can have code blocks initialized. Cannot use static code blocks such as: Static{}name = "haha";} public void Eat () {System.out.println ("Chew Bones");} public void Show () {System.out.println (name);}}. Show ();; Animal dog = new Animal () {//Implementation call multiple public void Eat () {System.out.println ("Chew Bones");}}; P.feed (dog);p. Feed (dog);p. Feed (new Animal () {public void Eat () {System.out.println ("eat Fish");}});} Class Person{public void Feed (Animal Animal) {animal.eat ();}} Abstract class Animal{public abstract void Eat ();} /*class Dog extends animal{public void Eat () {System.out.println ("Chew Bones");}} */
Anonymous inner class of member inner class of Java inner class