I’ve a code:
interface My {
//this is left empty
}
class Test implements My {
public static void main(String[] args){
My m=new Test();
String str=m.toString(); //how reference variable m calls toString() in Test even it is not in My?
System.out.println(str);
str=m.show(); //Error:
System.out.println(str);
}
public String toString(){
return "Hello";
}
public String show(){
return "World";
}
}
The error at str=m.show() is cannot find symbol str=m.show() and location: variable m of type My.
My question is how the m.toString() does not report any error but m.show() report such error.
toString()is defined inObject. Any concrete implementation of an interface will also extendObject, so you can call any method declared inObject.