Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4218669

Library classes (String, etc.)declared final, instead of methods declared final.

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.0
    • core-libs
    • generic
    • generic



      Name: dbT83986 Date: 03/09/99


      The java.lang.String class is currently declared as:

      final class String { ... }

      This means that I cannot create my own derived classes from it.

      I understand that the methods of the String class methods need to be declared 'final' for efficiency reasons. This, of course,
      means that they cannot be overridden by subclasses. However,
      there are still other valid reasons to want to inherit from a
      String object.

      In particular, I would really like to do this, because I have a variety of different kinds of Strings in my program which have
      different semantics (UserName, FileName, etc.) and which I would
      like to be able to use the strong type-checking in Java to distinguish from each other. These are conceptually distinct types which happen to be implemented as String objects because
      they require the same sort of handling, methods, etc. as Strings.
      The obvious object-oriented solution, then, would be to have them be derived classes of String so that I could declare methods such as:
           class UserName extends String { ... }
           class PassWord extends String { ... }
           class MyNeatoClass
           {
              // Can't pass a normal String here-- must use the
              // strongly-typed version.
              void askUserForPasswordAndVerify(UserName x, PassWord y);
           }

      Unfortunately, the only way to do this in Java appears to be to create classes which contain a String, and which contain forwarding functions for each and every method in the String interface. Either that, or they provide an accessor to themselves
      as a String and require the user to be writing userName.toString() all over the place. The relationship that I want to express
      is that a UserName IS a special kind of String, not that it CONTAINS a String. In particular, having it CONTAIN a String means that I cannot put the class in, for instance, a heterogenous container that contains String-derived objects and casts them back to their proper types when they are removed.

      Anyway, I can't see that making the String class itself final serves any purpose in this case. I would like this restriction to be lifted, so that the String class is declared like this:

      class String { final String trim(); ... }

      instead of like this, as it currently is:

      final class String { String trim(); ... }

      This issue applies to other runtime classes as well, such as Integer, Boolean, Vector, etc.
      (Review ID: 54800)
      ======================================================================

            apalanissunw Anand Palaniswamy (Inactive)
            dblairsunw Dave Blair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: