• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

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: 22821
132
Eclipse IDE Spring 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: 28360
99
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??
 
"I know this defies the law of gravity... but I never studied law." -B. Bunny Defiant tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic