Can anonymous inner class (anonymous internal class) be extends (inherited) other classes, and implements (implemented) interface (Interface )?

Source: Internet
Author: User

Http://blog.csdn.net/fhm727/article/details/5220003

1. What is an anonymous internal class?

An internal class exists in another class, while an anonymous internal class, as its name implies, is an internal class without a name.

 

2. Why do I need anonymous internal classes?

Each inner class can inherit an implementation class ). Therefore, inner class is not limited to whether the outer class has inherited from a certain implementation class.
If the capabilities provided by inner class "inherit from multiple concrete or abstract classes" are missing, some design and programming problems will become very tricky.

Therefore, from a certain perspective, inner class is a complete solution to multiple inheritance issues. Interface can solve some of these problems, but inner classes
In order to effectively and practically allow "multiple implementation )". That is to say, inner classes actually allows you to inherit multiple non-interfaces.

At this level, internal classes are generally instantiated and initialized through their parent classes or inherited interfaces, as shown in Figure 3. In this case, the internal class name (a) is redundant, therefore, the anonymous internal class is used.

3. How to Implement anonymous internal classes?

  the implementation of anonymous internal classes is very simple, if there is an interface  
interface a {public void method ();}
you can write the following statement to get the object of the implementation class of interface a
A = new A () {public void method () {system. out. println ("hehe") ;};
and {public void method () {system. out. PR Intln ("hehe") ;}}; defines an anonymous internal class
4. Answer we can see that an anonymous internal class must be implemented using one class or one interface. At this level, it can inherit other classes or implement interfaces, but it is impossible to use the extends or implements keyword..

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.