interface I { void show(); } class A implements I { void show() { System.out.println("class A"); } public static void main(String s[]) { I i=new A(); i.show(); i.toString(); } } Q> As interface I does not contain the abstract method toString() but still The following code gets compiled. How?
when super class variable is used to refer sub class obj then compiler first searches the similar method in the super class if not found gives error. here Interface does not contain the method toString().
ex=>
class A { void show() { System.out.println("show"); } } class B { void show() { System.out.println("show B"); } void display() { System.out.println("display B"); } public static void main(String s[]) { A a=new B(); a.show(); //will execute a.display(); //give error }