Results 1 to 4 of 4
- 09-13-2012, 05:35 PM #1
Member
- Join Date
- Apr 2010
- Location
- london UK
- Posts
- 53
- Rep Power
- 0
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?
- 09-13-2012, 05:39 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 09-13-2012, 07:03 PM #3
Member
- Join Date
- Apr 2010
- Location
- london UK
- Posts
- 53
- Rep Power
- 0
Re: mutable class extending into immutable class
you mean something like:
But then I have to do all those wrapped methods is there no other way?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 }
Also does recognize that fixedObject is immutable?
- 09-13-2012, 09:26 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
Similar Threads
-
How can I make this class immutable?
By fatabass in forum New To JavaReplies: 6Last Post: 12-18-2011, 10:08 PM -
Immutable code to mutable
By Tonyuk2 in forum Advanced JavaReplies: 8Last Post: 09-29-2011, 02:59 AM -
how to write user defined class as Immutable?
By srinivasmallabathula in forum New To JavaReplies: 3Last Post: 07-04-2011, 11:50 PM -
What is an Immutable Class
By maheshkanda in forum New To JavaReplies: 3Last Post: 02-06-2009, 08:12 PM -
EXTENDING the string class
By ferno in forum New To JavaReplies: 3Last Post: 12-11-2007, 09:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks