• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forcing implementing methods

 
Tian Zhang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use an abstract class instead of an interface.
 
Tian Zhang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Paul Clapham
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Might there be some way we can do this using J5 annotations??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic