Java face question Anonymous Inner class (anonymous inner Class) can extends (inherit) other classes, can implements (implement) interface (interface)?

Source: Internet
Author: User



1. What is an anonymous inner class?



An inner class, a class that exists inside another class, and an anonymous inner class, as the name implies, is an inner class without a name.






2. Why do I need anonymous internal classes?



Each inner class is capable of inheriting an implementation class (Implementation). Therefore, inner class is not limited by whether outer class has been inherited from an implementation class.



Some problems with design and programming can become tricky if the ability of inner class to inherit from multiple representational (concrete) or abstract classes is reduced.


So, looking at inner class from a point of view, you can say it's a complete solution to multiple inheritance problems. Interface can solve some of these problems, but inner classes can effectively and realistically allow multiple implementations to inherit (multiple implementation). In other words, inner classes actually allows you to inherit multiple non-interface. Using an inner class from this level is typically instantiated and initialized by its parent or inherited interface, as shown in 3, when the name of the inner class is superfluous, so The anonymous inner class is used

 





3, how to implement anonymous internal class?



The implementation of anonymous inner class is very simple, such as an interface
Interface A{
public void method();
}
At this time, you can write the following statement to get the object of the implementation class of interface a
A a=new A(){ public void method(){System.out.println("hehe");} };
And the {public void method() {system. Out. Println ("he");}}; is to define an anonymous inner class

 


4. Answers to questions

The above can be seen in the anonymous inner class must be implemented with a class or an interface, if at this level it can inherit other classes can also implement the interface,

But it is impossible to pass the extends or implements keyword.

Java face question Anonymous Inner class (anonymous inner Class) can extends (inherit) other classes, can implements (implement) interface (interface)?


Related Article

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.