13

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);
    }
}
Akhilesh Dhar Dubey
  • 2,152
  • 2
  • 25
  • 39
  • What happened when you tried it? Alternatively, how would you construct an implementation of the interface that didn't extend Object? – user207421 Aug 25 '12 at 18:09
  • actully i want to know that how toString() method called by an interface reference variable m. – Akhilesh Dhar Dubey Aug 25 '12 at 18:14
  • 1
    Because, as I hinted, you *can't* construct an implementation of the interface that *doesn't* extend Object. So any implementation declares or inherits a toString() method, so you can call it. – user207421 Aug 26 '12 at 08:11

8 Answers8

14

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

John Ericksen
  • 10,995
  • 4
  • 45
  • 75
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • 1
    if it default created a abstract public methods.. why are we not forced to implement the method in the concrete class which implements it???.. it does not give out any compilation exception.!!! in Short Interface A has override toString() from object. Class B implements IA. shouldn't class implement toString() ?? – Punith Raj Aug 29 '13 at 11:11
  • @PunithRaj If class B not extends any class then it is implicitly extends Object class. Object class having the implementation for that abstract methods. – Jinu P C Nov 19 '16 at 11:10
11

Object has a toString() method, so everything (except primitive types) has a toString() method. Java will treat anything, even an empty interface, as having all the methods of Object, because it always does.

John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
  • But,it is not written in java documentation that an interface extends an Object class. Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class. and let say, if an interface extends Object class so what about non abstract method which is defined in Object class.. and as i know that an interface can only extends interfaces, but here Object is not an interface.... ??? – Akhilesh Dhar Dubey Aug 25 '12 at 17:36
  • The op means how the toString() method was called on Interface reference... Interface never extends Object class.... – Kumar Vivek Mitra Aug 25 '12 at 17:36
  • 1
    @AkhileshDharDubey Interfaces don't extend classes—that's not possible. Java has a specific special case where an interface that doesn't extend any other interface implicitly includes all the methods on `Object`. And it *is* documented. – John Calsbeek Aug 25 '12 at 17:40
  • @John Calsbeek you are right at this...but the situation in which it does is in my answer... – Kumar Vivek Mitra Aug 25 '12 at 17:42
  • @KumarVivekMitra But if we do not declare String toHi(); method in My interface, It will give compiletion error. i.e. C.java:25: error: cannot find symbol String s1=m.toHi(); ^ symbol: method toHi() location: variable m of type My 1 error – Akhilesh Dhar Dubey Aug 25 '12 at 17:59
  • @AkhileshDharDubey first of all if you dont declare the toHi() method in Inteface it give `HELLO` as output, and if you remove toHi() from Interface and toString() method from class C, it gives the default reply of `getClass().getName()@hashCode` ie `(com.chat.C@360be0)`. I had to delete my answer because some ignorant user gave it a negative mark, before trying it out..and i am really sorry that i gave the correct answers to such ignorant people... – Kumar Vivek Mitra Aug 26 '12 at 05:05
  • 1
    @KumarVivekMitra A wrong downvote isn't much of a reason for deleting a correct answer. – user207421 Aug 26 '12 at 08:15
  • @EJP as you said it...i am undeleting my answer... – Kumar Vivek Mitra Aug 26 '12 at 10:47
  • @JohnCalsbeek how interfaces includes all the methods of Object class ? – Akhilesh Dhar Dubey Mar 16 '13 at 17:41
6

Any Object has a toString() method. Anything that would implement an interface will implicitly extend Object, so will also have a toString() method.

Claudiu
  • 224,032
  • 165
  • 485
  • 680
2

Class C implements My but it also extends Object, as all objects eventually do in their inheritance tree.

The Object class does have the method toString(), along with a number of others.

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
2

Since class Demo implicitly extends class Object, it inherits method toString. And since that's not an abstract method, class Demo is not forced to provide an implementation, although you're able to directly invoke toString on an instance of Demo. For more information, please see Lesson: Interfaces and Inheritance.

As stated in the Object API,

Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

Also, note that the toString method is not part of the interface definition, but rather the Object class definition.

Akhilesh Dhar Dubey
  • 2,152
  • 2
  • 25
  • 39
mre
  • 43,520
  • 33
  • 120
  • 170
2

The reason that you are able to invoke that method through that variable of your interfce is because of a special treatment in case of interfaces in Java.

Even though the method is not explicitly declared in the interface, the special treatment implicitly provides declarations for all the public instance methods defined in the class Object. And the toString method is one of them.

But, note that interfaces don't implicitly extend any super interface (or class) unlike classes which implicitly extend the Object class.

You will find a better explanation here - Do Interfaces really inherit the Object class in Java? .

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
1

toString is declared inside Object. When an Object implements an interface it must have a toString method.

Therefore any object reference, be it an interface or an enum must have all the object methods:

  • clone
  • equals
  • finalize
  • getClass
  • hashCode
  • notify
  • notifyAll
  • toString
  • wait
Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
0

Every Object(except primitive types) in java has a toString() method.

Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
  • primitive types are not objects (and you forgot to mention that interfaces are not objects either) – mre Aug 25 '12 at 18:30