0
 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 } 

4 Answers 4

7

All classes inherit from Object. Object has a toString.

To use any interface it must be backed by a actual class. So the Java compiler knows that it can use any method defined in java.lang.Object when dealing with an Interface.

To put it a slightly different way:

interface I { ... } 

has an "magic"

interface I extends Object { ... } 

So you can use Objects methods when detail with I. However you can not use any methods in the concrete class that do not appear in the interface. So to combine you two examples:

interface Car { void drive(); } class Convertible implements Car { void drive() {} void openRoof() {} public static void main() { Car porscheBoxster = new Convertible(); porscheBoxster.drive(); // OK - exists in interface porscheBoxster.toString(); // OK - exists in java.lang.Object. porscheBoxster.openRoof(); // Error. All we know is the porscheBoxster is of type Car. // We don't know if it is a Convertible or not. } } 
Sign up to request clarification or add additional context in comments.

2 Comments

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 A"); } } 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 }
All classes inherit from Object in Java. So Java knows when it encounters an interface it will be backed by a concrete class and so will have the methods defined in java.lang.Object
3

Every class in Java is an Object, thus, they are always able to run the following methods:

  • clone()
  • equals(Object)
  • finalize()
  • getClass()
  • hashCode()
  • notify()
  • notifyAll()
  • toString()
  • wait()
  • wait(long)
  • wait(long, int)

Comments

2

Because 'toString()' is in the class Object which every non-primitive data is derived from. So every object has this method.

Comments

1

In Java, every class you construct, inherits from the base class Object. This means that your class by default will have a lot of useful methods, amongst others toString().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.