How is it possible to call toString method using the reference variable of interface Test, which does not have a toString method?
interface Test
{
void show();
String toHi();
}
class Demo implements Test
{
public void show(){
System.out.println("Show");
}
public String toString(){
return "Hello";
}
public String toHi(){
return "Hi";
}
public static void main(String[] args)
{
Test t=new Demo();
String s=t.toString();
System.out.println(s);
}
}
The the Java Documentation says…
When an interface has no direct SuperInterface, it will create abstract public method for all those public methods present in the Object class.This is why you are able to call the
toString()method on the interface reference