Very basic question - implementing an interface with toString
Hello all,
This is a very basic question. In eclipse, I have created an interface with three abstract methods 1. deepCopy() 2. compareTo() 3. toString().
I create a class that implements the interface, but when I do only the first two methods are generated and toString is not. This is not a critical problem as I can just type out the method declaration for toString but I just wanted to know why the signature doesn't appear on its own like every other method I declare in the interface.
Thank you
Re: Very basic question - implementing an interface with toString
Quote:
I create a class that implements the interface, but when I do only the first two methods are generated and toString is not.
I guess you are talking about using an IDE to create your source code. You tell the IDE you are about to write a class that implements your interface and it helpfully gives you two of the three methods declared by the interface. To repeat: this is something to do with the IDE (Netbeans, Eclipse or whatever).
To see why the IDE does this it might help to realise that declaring the toString() method in your interface does nothing at all. And that's because instances of your new class will also be instances of the class Object, and Object already has a toString() method declared. I'm guessing that your IDE gives you "stubs" for the additional methods declared by your interface, but ignores the method that the interface is merely redeclaring.
(This behaviour may be configurable depending on the IDE you are using.)
Re: Very basic question - implementing an interface with toString
All classes inherit the toString method from the Object class. Having an abstract toString method in your interface is pointless.
Re: Very basic question - implementing an interface with toString
I understand. I guess the point would be to have it show up in the implementing class to remind the programmer to code toString to suit his application and not use the built in method. If he doesn't see the toString declaration than he may forget to put one in.
Re: Very basic question - implementing an interface with toString
The problem is that for many classes there lots of methods that could be overridden - most of which you don't want to override. So the resulting stubs would be noise you just had to delete again.
The methods actually declared in an interface you are extending are different: they *have* to be implemented, or your class made abstract.