Sample Code for internal classes and interfaces:
Interface TestInterface {
Void sayHello ();
}
Public class Test {
// This is an internal class
Private class TestInnerClass {
//
}
Public void say (){
// This is an internal Anonymous class
New TestInterface (){
Public void sayHello (){
System. out. println ("hello world! ");
}
}. SayHello (); // the object of the anonymous internal class calls the sayHello () method.
}
Public static void main (String [] args ){
New Test (). say ();
}
}
In the above Code: we know that the interface cannot be instantiated, but this code is not actually an interface instantiation. You can understand it as follows:
// This is an internal Anonymous class
New TestInterface (){
Public void sayHello (){
System. out. println ("hello world! ");
}
}. SayHello (); // the object of the anonymous internal class calls the sayHello () method.
It can be understood that a class (anonymous) implements the TestInterface interface, implements the sayHello () method, and then calls sayHello () through the object of the anonymous class () method. So running this code output hello world!
Author "loading"