• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

forcing implementing methods

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use an abstract class instead of an interface.
 
Tian Zhang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 )
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Sheriff
Posts: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Might there be some way we can do this using J5 annotations??
reply
    Bookmark Topic Watch Topic
  • New Topic