Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Why a class( ex. String Class) should be final to protect its behaviour ?

800320Aug 13 2008 — edited Sep 17 2008
I read in a book about string class as follows
"*String class is declared as final. Because it is final, no one can subclass it and alter its behavior*"

My question is, "how the behaviour of String class can be altered if it is not final ?"

As per my knowledge in Java and inheritance in particular:

When a class is final it can not be inherited. If it is not final, some class might inherit it, and override String class methods. how this is going to change the behaviour of methods in String class?.

When an object is created for String class, with that object one can execute only the code in the String class not the code in the sub-class.Then the pre-defined behaviour of String class is not going to change.

Can any one please explain how one can change the behaviour of String class if it is not final?

"I have also heard that hackers could substitiute some class for String class" how could they?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 15 2008
Added on Aug 13 2008
21 comments
707 views