I am confused on one scenario of interface.Below is the code in which I haven't declare the toString()
method in interface.
It is the method of object class.But still toString()
method is able to execute from the parent class reference variable .But the rule says that before calling the child class method it first look method in interface, if method is present then call the child class method but in this scenario How toString()
is executed without declaration in interface
please explain me
public interface Parent {
void show();
}
class Base implements Parent {
public void show() {
System.out.println("hey it is going to be execute");
}
public String toString() {
return "itspossible";
}
public static void main(String[] args) {
Parent parent = new Base();
System.out.println(parent.toString());
}
}