Can we create an instance of an interface in Java? -


this question has answer here:

is possible create instance of interface in java?

somewhere have read using inner anonymous class can shown below:

interface test   {       public void wish();   }   class main   {       public static void main(string[] args)       {           test t=new test()           {               public void wish()               {                   system.out.println("output: hello how r u");               }           };       t.wish();       }   }      cmd> javac main.java   cmd> java main   output: hello how r u   

is correct here?

yes, example correct. anonymous classes can implement interfaces, , that's time can think of you'll see class implementing interface without "implements" keyword. check out code sample right here:

interface programmerinterview  {     public void read(); }  class website  {     programmerinterview p = new programmerinterview () {         public void read() {             system.out.println("interface programmerinterview class implementer");        }      }; } 

this works fine. taken page:

http://www.programmerinterview.com/index.php/java-questions/anonymous-class-interface/


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -