Results 1 to 5 of 5
  1. #1
    rmohindra is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Very basic question - implementing an interface with toString

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

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default 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.

  4. #4
    rmohindra is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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.

Similar Threads

  1. NEED HELP in IMPLEMENTING A GIVEN INTERFACE!
    By wasiqjaved in forum New To Java
    Replies: 0
    Last Post: 10-03-2011, 01:40 PM
  2. Implementing Interface
    By mew in forum New To Java
    Replies: 4
    Last Post: 02-16-2010, 04:33 PM
  3. help! implementing an interface
    By manda147 in forum New To Java
    Replies: 28
    Last Post: 11-17-2008, 05:27 AM
  4. Implementing an interface
    By bugger in forum Advanced Java
    Replies: 1
    Last Post: 01-09-2008, 02:35 PM
  5. Implementing Serializable interface
    By javaplus in forum Advanced Java
    Replies: 4
    Last Post: 12-18-2007, 01:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •