• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

JAVA: Can you add methods to a JFC base class? ( String )

Superwormy

Golden Member

String is a base class in Java, is there a way I can add a method to that class?
I know I could do this:

class NewString extends String { /* new method here */ }

But I was kinda thinking that you could add methods to an already defined class later... am I wrong?
 
You can't overide string because it was deliberately designed to not be overwritable. This has something to do with security concerns. You could use a string as a member and then re write lots of methods.. fun fun fun

The way of declaring it is something like Public Class B Extends Class A { /* method here */}

==========================
java.lang
Class String
java.lang.Object
java.lang.String

All Implemented Interfaces:
CharSequence, Comparable, Serializable

--------------------------------------------------------------------------------

public final class String
extends Object
implements Serializable, Comparable, CharSequence
===================================

When it says final you can't extend it.



Java Api
 
Inheriting something like a builtin string class is kind of frowned upon. Make a class which manipulates strings. StringManipulator or something. 😉
 
Originally posted by: michaelh20
You can't overide string because it was deliberately designed to not be overwritable. This has something to do with security concerns. You could use a string as a member and then re write lots of methods.. fun fun fun

The way of declaring it is something like Public Class B Extends Class A { /* method here */}

==========================
java.lang
Class String
java.lang.Object
java.lang.String

All Implemented Interfaces:
CharSequence, Comparable, Serializable

--------------------------------------------------------------------------------

public final class String
extends Object
implements Serializable, Comparable, CharSequence
===================================

When it says final you can't extend it.



Java Api

Good call, can't believe I missed that...although I've never bothered overriding String before.

But if you want to use a class that manipulates strings, then you could probably use StringBuffer.
 
Back
Top