java-Anonymous Inner class

Source: Internet
Author: User

Anonymous inner class is also an inner class without a name

Because there is no name, the anonymous inner class can only be used once, and it is often used to simplify code writing

But there is also a precondition for using anonymous inner classes: You must inherit from a parent class or implement an interface

Example 1: Do not use anonymous inner classes to implement abstract methods?
12345678910111213141516 abstract class Person {    public abstract void eat();}class Child extends Person {    public void eat() {        System.out.println("eat something");    }}public class Demo {    public static void main(String[] args) {        Person p = new Child();        p.eat();    }}

running result:eat something

As you can see, we inherit the person class with child, and then we implement an instance of child and transform it upward into a reference to the person class.

However, if the child class here is used only once, wouldn't it be a hassle to write it as a separate class?

The anonymous inner class is introduced at this time.

Example 2: The basic implementation of an anonymous inner class?
1234567891011121314 abstract class Person {    public abstract void eat();}public class Demo {    public static void main(String[] args) {        Person p = new Person() {            public void eat() {                System.out.println("eat something");            }        };        p.eat();    }}

running result:eat something

As you can see, we directly implement the methods in the abstract class person in curly braces.

This allows you to omit the writing of a class

Also, anonymous inner classes can be used on interfaces

Example 3: Use an anonymous inner class on an interface?
interface Person {    public void eat();}public class Demo {    public static void main(String[] args) {        Person p = new Person() {            public void eat() {                System.out.println("eat something");            }        };        p.eat();    }}

running result:eat something

As can be seen from the above example, as long as a class is abstract or an interface, the methods in its subclasses can be implemented using anonymous inner classes

The most common situation is in the implementation of multithreading, because to implement multithreading must inherit the thread class or inherit the Runnable interface

Example of an anonymous inner class implementation of the 4:thread class?
public class Demo {    public static void main(String[] args) {        Thread t = new Thread() {            public void run() {                for (int i = 1; i <= 5; i++) {                    System.out.print(i + " ");                }            }        };        t.start();    }}

operating Result:1 2 3 4 5

An anonymous inner class implementation of the instance 5:runnable interface?
12345678910111213 public class Demo {    public static void main(String[] args) {        Runnable r = new Runnable() {            public void run() {                for (int i = 1; i <= 5; i++) {                    System.out.print(i + " ");                }            }        };        Thread t = new Thread(r);        t.start();    }}

operating Result:1 2 3 4 5

java-Anonymous 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.