Interface cannot be instantiated

Source: Internet
Author: User

Today, when I look at the template method design pattern, we find that in the interface callback, the anonymous class has a new interface. Thought it was weird, and I wrote a little program to test it.

// Hibernatecallback is an interface  Public void Saveuser (final  user user) {        execute (new  hibernatecallback () {              public Object Doinsession (Session session) {                session.save (user);                 return NULL ;            }        });    }

Here is the test code

 Public Interfaceinterface{voidprint ();} Public classTest {Static voidUpdateUser () {Execute (NewInterface () {@Override Public voidprint () {System.out.println ("I am an Interface");    }                    }); }          Static voidExecute (Interface i) {i.print (); }         Public Static voidMain (string[] args) {updateUser (); }}    
The output is:  I am an interface

Anti-compilation files for anonymous classes

From the anti-compilation file is not a new interface, but this anonymous class implementation of the interface, and then passed on as a parameter of the past.

Interface cannot be instantiated

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.