• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

forcing implementing methods

 
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 ]
 
Sheriff
Posts: 22041
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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 )
 
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.

 
Marshal
Posts: 26128
77
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 ]
 
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.
 
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??
 
Why am I so drawn to cherry pie? I can't seem to stop. Save me tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic