Two Laptop Bag*
The moose likes Beginning Java and the fly likes forcing implementing methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "forcing implementing methods" Watch "forcing implementing methods" New topic
Author

forcing implementing methods

Tian Zhang
Greenhorn

Joined: Mar 23, 2005
Posts: 24
hi there!

is there a way to force implemented classes to override methods inherent in all object such as toString() etc... i.e.:


is still valid, i suppose as toString() is already extended from Object.

thanks!
tian
[ November 14, 2005: Message edited by: Tian Zhang ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Use an abstract class instead of an interface.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tian Zhang
Greenhorn

Joined: Mar 23, 2005
Posts: 24
i'll do that in the next iteration.

thanks rob,

tian

(it's been a while since i last coded, things are coming back *slowly* but surely )
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Tian Zhang:
hi there!

is there a way to force implemented classes to override methods inherent in all object such as toString() etc... i.e.:


is still valid, i suppose as toString() is already extended from Object.

thanks!
tian

[ November 14, 2005: Message edited by: Tian Zhang ]


To answer your question, "all classes implicitly override toString()". You are stumbling upon an unfortunate defect in the language/API. What you might want to do is redefine your own abstraction, but you lose some of the functionality in the core API. You can workaround this with an adapter.



Tony Morris
Java Q&A (FAQ, Trivia)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Even if you could force the writer of a class to implement the toString() method, there are still passive-aggressive implementations likeorso that probably isn't going to solve whatever problem it was that caused you to ask the question.
Tian Zhang
Greenhorn

Joined: Mar 23, 2005
Posts: 24
thanks for the suggestion tony, though i will probably refactor the class in a different way during the next iteration.

---

there is really no guarantee that the developer will implement (or override) sensably for any methods. i am hoping that "bringing" toString() method to their attention is enough for them to do a sufficient job. i'm working on the assumption that a "crude" toString() is perhaps better then the default object address.

thanks,
tian
[ November 14, 2005: Message edited by: Tian Zhang ]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Tian Zhang:
thanks for the suggestion tony, though i will probably refactor the class in a different way during the next iteration.

---

there is really no guarantee that the developer will implement (or override) sensably for any methods. i am hoping that "bringing" toString() method to their attention is enough for them to do a sufficient job. i'm working on the assumption that a "crude" toString() is perhaps better then the default object address.

thanks,
tian

[ November 14, 2005: Message edited by: Tian Zhang ]


It sounds like you're trying to force developers to implement their own toString() whether they like it or not because it's generally wise to do so. On a purely intellectual level, I don't see how this is going to work. If a programmer fails to implement toString() when appropriate then they are more than likely going to fail to do so in an appropriate way when you force them to. The amount of effort involved to essentially "remind" a programmer that they should implement toString() makes it unrealistic in my opinion.
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
Might there be some way we can do this using J5 annotations??


ASCII silly question, Get a silly ANSI.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forcing implementing methods
 
Similar Threads
K&B - Erratum ?
Final Variables immutable class
ch8 K&B 1.4 qno:8
generic and arrays
Object reference contructor