Can we create an instance of an interface in Java? -
this question has answer here:
- can create object of interface? 5 answers
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
Post a Comment