Anonymous inner class of member inner class of Java inner class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.