wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overiding methods from String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overiding methods from String" Watch "Overiding methods from String" New topic
Author

Overiding methods from String

Daniel Martins
Ranch Hand

Joined: Oct 22, 2009
Posts: 45
Hello Ranchers

Being String a final class, how is it possible to override a method like toString() ?


Daniel Martins
Ranch Hand

Joined: Oct 22, 2009
Posts: 45
Ok, it just occured to me that if we do not extend String (because we can't), we are basically creating a new method called toString with return type String.

The original one is not visible from within the class...

Am I rigth here...?

Cheers
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
It can still be called via "super.toString()", though.


Ping & DNS - my free Android networking tools app
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Daniel Martins wrote:Hello Ranchers

Being String a final class, how is it possible to override a method like toString() ?




Daniel, a quick reminder is that the toString() method is inherited by all classes from the Object class not the String class. That toString() method is not final and therefore you can override it till the cows come home.


SCJP 6
Daniel Martins
Ranch Hand

Joined: Oct 22, 2009
Posts: 45
Ok, I made a wrong assumption.

Thank you!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Overiding methods from String