Results 1 to 4 of 4
  1. #1
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default mutable class extending into immutable class

    What is the right/ best way to do this?

    I have two classes.

    A class Varies that has a lot of methods and where the importand private fields are mutable,

    A class Fixed that is an extention of class Varies but where the fields that it inherits from class Varies have to be immutable. (the whole object of class Fixed should be immutable)

    I was thinking to do this by overiding the methods of Varies in Fixed but that doesn't look right. (what if I later add an new changing method to Varies and forget to override it?)

    Treating them as unrelated classes also don't seem right, it means copying most methods from Varies to Fixed

    What is the right/ best way to do this?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: mutable class extending into immutable class

    You don't extend.
    You wrap.

    Since Varies will have methods that allow you to change the fields, and you cannot change those methods to private in Fixed, you have no way of making it immutable via inheritance. So wrap it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    willemien is offline Member
    Join Date
    Apr 2010
    Location
    london UK
    Posts
    53
    Rep Power
    0

    Default Re: mutable class extending into immutable class

    you mean something like:

    Java Code:
    public class Fixed
    {
        private fixed Varies fixedObject
    
        public Fixed(Varies v)
        {
            fixedObject = new Varies(v.toString)    
        }
    
         
        public Fixed(String s)
        {
            fixedObject = new Varies(s)    
        }
        
        private Fixed()
        {
            // no public empty constructor    
        }
    
        public toVaries()
        {
            return = new Varies(fixedObject.toString)    
        }
         
        // wrapped methods from varies 
        public int getLen {return fixedObject.getLen} 
        public char getFirst {return fixedObject.getFirst} 
        public String toString {return fixedObject.toString}
        // ect
    }
    But then I have to do all those wrapped methods is there no other way?
    Also does recognize that fixedObject is immutable?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: mutable class extending into immutable class

    fixedObject won't be immutable. Varies is not an immutable class.
    But whatever object of class Fixed that is created that contains fixedObject will be immutable.

    This is the standard pattern for removing an objects setters, though the above won't actually compile.
    It does work better if both Varies and Fixed share an interface, but that's often not possible.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. How can I make this class immutable?
    By fatabass in forum New To Java
    Replies: 6
    Last Post: 12-18-2011, 11:08 PM
  2. Immutable code to mutable
    By Tonyuk2 in forum Advanced Java
    Replies: 8
    Last Post: 09-29-2011, 03:59 AM
  3. how to write user defined class as Immutable?
    By srinivasmallabathula in forum New To Java
    Replies: 3
    Last Post: 07-05-2011, 12:50 AM
  4. What is an Immutable Class
    By maheshkanda in forum New To Java
    Replies: 3
    Last Post: 02-06-2009, 09:12 PM
  5. EXTENDING the string class
    By ferno in forum New To Java
    Replies: 3
    Last Post: 12-11-2007, 10:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •