java - Can I Create Object For Interface? -
is possible creating object interface, if yes how can we? according view following code says can:
runnable r= new runnable(){ // implementation }
this not creating instance of interface, creating class implements interface. when write
runnable runnable = new runnable() { @override public void run() { // todo auto-generated method stub } };
you creating class implementing runnable interface. need follow rules here, here, overriding run method runnable
there similar thing abstract class also. can test using example public abstract class abstractclass { public void somemethod(){ system.out.println("abstract class"); } }
and class i.e. testclass
public class testclass { public static void main(string[] args) { abstractclass abstractclass = new abstractclass() { public void somemethod() { system.out.println("concrete class method"); } }; abstractclass.somemethod(); } }
this create instance of subclass in overriding somemethod()
; program prints
concrete class method
this proves creating instance of subclass.
Comments
Post a Comment