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.
 }
}
answered Sep 21, 2009 at 10:28
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)
answered Sep 21, 2009 at 10:31

Comments

2

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

answered Sep 21, 2009 at 10:28

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().

Chii
14.8k3 gold badges40 silver badges47 bronze badges
answered Sep 21, 2009 at 10:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.