Do interfaces have toString method?
How is it possible to call toString method using the reference variable of interface My, which does not have a toString method?
interface My
{
void show();
String toHi();
}
class C implements My
{
public void show(){
System.out.println("show\n");
}
public String toString(){
return "HELLO";
}
public String toHi(){
return "Hi";
}
public static void main(String[] args)
{
My m=new C();
String s=m.toString();
System.out.println(s);
}
}
How is it possible to call toString method using the reference variable of interface My, which does not have a toString method?
interface My
{
void show();
String toHi();
}
class C implements My
{
public void show(){
System.out.println("show\n");
}
public String toString(){
return "HELLO";
}
public String toHi(){
return "Hi";
}
public static void main(String[] args)
{
My m=new C();
String s=m.toString();
System.out.println(s);
}
}
No comments:
Post a Comment