Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading user defined class

 
siva prakash
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can i override my own String class in place of sun's provided String class and use as normally .

ie code snippet will be like this,

class String {

String toString() {
// my own functionality
}
// some methods as in normal String class( provided by sun)
}

public class Main {

String st ; // here my own implementation should be used not sun's

}

}
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you define a class called String you're not overriding anything, you've
just happened to give your class the same name as a class in another package,
namely java.lang.String.

Now *is it a good idea* to do this? Most programmers have such universal
classes hardwired into their brains: String == java.lang.String, so I'd
suggest calling your class something else, like MyString...

This reminds me of the time I was trying to help a student of mine who
had a mysterious error in his cloning code -- the interface Cloneable
just didn't seem to be working right. Then I discovered he had inadvertantly (?)
define his own interface Cloneable!

Also note, by the way, that you can't subclass java.lang.String, not matter
what you call the subclass -- String is final.
[ December 15, 2005: Message edited by: Jeff Albrechtsen ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What requirement are you trying to satisfy by doing this? It appears all you want to do is override toString() which you can do in any object.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by siva prakash:
Can i override my own String class in place of sun's provided String class and use as normally .


The short answer is: Yes, actually. You need to make sure the compiled class is on the special boot class path, rather than on the normal classpath. You'd want to start Java using something like

java -bootclasspath c:/mydir;c:/jdk1.5.0/jre/lib/rt.jar MyProgram

and then make sure that java/lang/String.class was in "c:/mydir".

The longer answer is that if you're writing a special tool -- a debugger, or profiler, or something else that has to do weird things -- then this might be called for. Or if you're doing this temporarily while hacking on a program to instrument something to help you understand what's going on, then that's fine: I've done that kind of thing myself.

But in normal programming, it's terrible style; and of course in many environments you won't even be able to do it, because you won't have access to the boot classpath.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic