Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Loading user defined class

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

}

}
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic